-
Notifications
You must be signed in to change notification settings - Fork 233
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewed 1 of 1 files at r1, all commit messages.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @emawby, @Jeasmine, @nan-li, and @tanaynigam)
android/src/main/java/com/onesignal/flutter/OneSignalPlugin.java, line 109 at r1 (raw file):
public void onAttachedToActivity(@NonNull ActivityPluginBinding binding) { // onAttachedToEngine is called first that sets the BinaryMessenger so we can pass null here. init(
onAttachedToEngine
already calls init
, adding this means are will be calling init
more than once. Some of the logic such as channel = new MethodChannel(this.messenger, "OneSignal");
doesn't look like it should be called more than once in there.
Some options the come to mind however are check if init is being called more than once, or to split up this context gathering and calling OneSignal.initWithContext
.
After the changes to support Android V2 embedding, the plugin is set up in `onAttachedToEngine` and the Application Context is not an instance of Activity. When this context is eventually passed to `setAppId`, we end up with the current activity is null which leads to some problems. This is only a problem on the first run of the app. This commit implements the `ActivityAware` interface and in `onAttachedToActivity`, updates the context to the new activity (https://docs.flutter.dev/development/packages-and-plugins/plugin-api-migration). In addition, use `activeContext()` instead of `context()` for the registrar for Flutter's v1 embedding to get the application context. Doing some testing shows the former gives, for example, the `MainActivity` while the latter gives `FlutterApplication`.
fe03fa0
to
c319281
Compare
I think it suffices to update |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewed 1 of 1 files at r2, all commit messages.
Reviewable status:complete! all files reviewed, all discussions resolved (waiting on @emawby, @Jeasmine, and @tanaynigam)
android/src/main/java/com/onesignal/flutter/OneSignalPlugin.java, line 109 at r1 (raw file):
Previously, nan-li (Nan) wrote…
I think it suffices to update
this.context
with the application context when we receiveonAttachedToActivity
. As far as I can tell, thethis.context
is only ever used byinitWithContext()
which should happen later. In some simple testing,onAttachedToActivity
is called immediately afteronAttachedToEngine
.
Good idea, and is less code too.
|
||
@Override | ||
public void onDetachedFromActivity() { | ||
} |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewed 1 of 1 files at r2, all commit messages.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @emawby, @Jeasmine, and @nan-li)
Description
One Line Summary
Implement the Flutter
ActivityAware
interface to pass theActivity
context instead ofApplication
context tosetAppId
.Details
Motivation
Fixes #509. What we were experiencing was that on the first run of the app, the current activity would be
null
so that IAMs don't display and the app is sensed as in the background even when in the foreground. Backgrounding the app and returning resumed normal behavior.Why? After the changes to support Android V2 embedding, the plugin is set up in
onAttachedToEngine
and the Application Context is not an instance of Activity.When this context is eventually passed to
setAppId
, we end up with the current activity isnull
which leads to some problems. Note that this is only a problem on the first run of the app.This PR implements the
ActivityAware
interface and inonAttachedToActivity
, updates the existing context to the activity received, following some Flutter documentation here.In addition, use
activeContext()
instead ofcontext()
for the registrar for Flutter's v1 embedding to get the application context. Doing some testing shows the former provides theMainActivity
while the latter providesFlutterApplication
.Scope
During testing,
onAttachedToActivity
is called right afteronAttachedToEngine
so we expect to get the activity context immediately.There are 3 other
ActivityAware
methods are not implemented as they don't appear to be needed. We only need the first call to Flutter'sonAttachedToActivity
during the first run of the app. After that, the OneSignalActivityLifecycleHandler
should be triggered and set the activities.Testing
Unit testing
N/A
Manual testing
Tested on Pixel 5 emulator on API 30 and checking logs for
onAttachedToEngine
andonAttachedToActivity
and what context is passed tosetAppId
. Also put logging for the other 3 methods inActivityAware
and they are never logged.Affected code checklist
Checklist
Overview
Testing
Final pass
This change is