Skip to content

Use ActivityAware to get the Activity context #534

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 15, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion android/src/main/java/com/onesignal/flutter/OneSignalPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import android.annotation.SuppressLint;
import android.content.Context;

import io.flutter.embedding.engine.plugins.activity.ActivityAware;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;

import com.onesignal.OSDeviceState;
import com.onesignal.OSEmailSubscriptionObserver;
import com.onesignal.OSEmailSubscriptionStateChanges;
Expand Down Expand Up @@ -42,6 +45,7 @@ public class OneSignalPlugin
extends FlutterRegistrarResponder
implements FlutterPlugin,
MethodCallHandler,
ActivityAware,
OneSignal.OSNotificationOpenedHandler,
OneSignal.OSInAppMessageClickHandler,
OSSubscriptionObserver,
Expand Down Expand Up @@ -96,12 +100,29 @@ private void onDetachedFromEngine() {
OneSignal.setInAppMessageClickHandler(null);
}

@Override
public void onAttachedToActivity(@NonNull ActivityPluginBinding binding) {
this.context = binding.getActivity();
}

@Override
public void onDetachedFromActivity() {
}
Copy link
Contributor

@westy92 westy92 Feb 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should our Activity reference be cleared here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your input @westy92,
Since we only need to get the activity on initialization and use the activity only for setAppId, we don't to track it after.


@Override
public void onReattachedToActivityForConfigChanges(@NonNull ActivityPluginBinding binding) {
}

@Override
public void onDetachedFromActivityForConfigChanges() {
}

// This static method is only to remain compatible with apps that don’t use the v2 Android embedding.
@Deprecated()
@SuppressLint("Registrar")
public static void registerWith(Registrar registrar) {
final OneSignalPlugin plugin = new OneSignalPlugin();
plugin.init(registrar.context(), registrar.messenger());
plugin.init(registrar.activeContext(), registrar.messenger());

// Create a callback for the flutterRegistrar to connect the applications onDestroy
registrar.addViewDestroyListener(new PluginRegistry.ViewDestroyListener() {
Expand Down