Skip to content

Commit

Permalink
Prevent init onHostResume if already initialized for that activity co…
Browse files Browse the repository at this point in the history
…ntext

Previously we were entering into the init function and logging the error "Already initialized..." which was reported as a bug.
  • Loading branch information
rgomezp committed Sep 7, 2023
1 parent 14366f0 commit 252fb3b
Showing 1 changed file with 10 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -154,22 +154,15 @@ public RNOneSignal(ReactApplicationContext reactContext) {
}

// Initialize OneSignal only once when an Activity is available.
// React creates an instance of this class to late for OneSignal to get the current Activity
// React creates an instance of this class too late for OneSignal to get the current Activity
// based on registerActivityLifecycleCallbacks it uses to listen for the first Activity.
// However it seems it is also to soon to call getCurrentActivity() from the reactContext as well.
// This will normally succeed when onHostResume fires instead.
private void initOneSignal() {
oneSignalInitDone = true;
OneSignal.sdkType = "react";
Context context = mReactApplicationContext.getCurrentActivity();

if (oneSignalInitDone) {
Log.e("OneSignal", "Already initialized the OneSignal React-Native SDK");
return;
}

oneSignalInitDone = true;


if (context == null) {
// in some cases, especially when react-native-navigation is installed,
// the activity can be null, so we can initialize with the context instead
Expand Down Expand Up @@ -844,7 +837,14 @@ public void onHostPause() {

@Override
public void onHostResume() {
initOneSignal();
// Initialize OneSignal only once when an Activity is available.
// React creates an instance of this class too late for OneSignal to get the current Activity
// based on registerActivityLifecycleCallbacks it uses to listen for the first Activity.
// However it seems it is also to soon to call getCurrentActivity() from the reactContext as well.
// This will normally succeed when onHostResume fires instead.
if (!oneSignalInitDone) {
initOneSignal();
}
}

@Override
Expand Down

0 comments on commit 252fb3b

Please sign in to comment.