-
Notifications
You must be signed in to change notification settings - Fork 379
Expand file tree
/
Copy pathActivityLifecycleTracker.java
More file actions
72 lines (59 loc) · 2.56 KB
/
ActivityLifecycleTracker.java
File metadata and controls
72 lines (59 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package com.onesignal.rnonesignalandroid;
import android.app.Activity;
import android.app.Application;
import android.os.Bundle;
import androidx.annotation.Nullable;
import java.lang.ref.WeakReference;
/**
* Tracks the host app's current Activity from Application.onCreate onward.
*
* <p>Registered very early via {@link OneSignalInitializer} (androidx.startup) so it captures the
* first {@code MainActivity.onResume} that fires before the React Native bridge has loaded the JS
* bundle. Without this, {@link com.facebook.react.bridge.ReactApplicationContext#getCurrentActivity()}
* frequently returns {@code null} during cold start in bridgeless mode, causing
* {@code RNOneSignal.initialize} to hand the OneSignal SDK an ApplicationContext instead of the
* real Activity. That in turn leaves {@code ApplicationService.current == null} and queues
* {@code requestPermission()} until the next foreground.
*/
public class ActivityLifecycleTracker implements Application.ActivityLifecycleCallbacks {
private static final ActivityLifecycleTracker INSTANCE = new ActivityLifecycleTracker();
private volatile WeakReference<Activity> currentActivity = new WeakReference<>(null);
private ActivityLifecycleTracker() {}
public static ActivityLifecycleTracker getInstance() {
return INSTANCE;
}
@Nullable
public Activity getCurrentActivity() {
return currentActivity.get();
}
@Override
public void onActivityCreated(Activity activity, @Nullable Bundle savedInstanceState) {
currentActivity = new WeakReference<>(activity);
}
@Override
public void onActivityStarted(Activity activity) {
currentActivity = new WeakReference<>(activity);
}
@Override
public void onActivityResumed(Activity activity) {
currentActivity = new WeakReference<>(activity);
}
@Override
public void onActivityPaused(Activity activity) {
// Intentionally no-op: keep the reference so a transient overlay (e.g. permission dialog,
// PermissionsActivity) doesn't blank out the current Activity for callers that race with it.
}
@Override
public void onActivityStopped(Activity activity) {
// Intentionally no-op for the same reason as onActivityPaused.
}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {}
@Override
public void onActivityDestroyed(Activity activity) {
Activity current = currentActivity.get();
if (current == activity) {
currentActivity = new WeakReference<>(null);
}
}
}