@@ -97,19 +97,27 @@ private void configureNetworkMonitoring() {
97
97
}
98
98
99
99
private void configureBackgroundStateListener () {
100
- Application applicationContext = (Application ) context .getApplicationContext ();
100
+ Application application = (Application ) context .getApplicationContext ();
101
101
final AtomicBoolean inBackground = new AtomicBoolean ();
102
102
103
103
// Manually register an ActivityLifecycleCallback. Android's BackgroundDetector only notifies
104
104
// when it is certain that the app transitioned from background to foreground. Instead, we
105
105
// want to be notified whenever there is a slight chance that this transition happened.
106
- applicationContext .registerActivityLifecycleCallbacks (
106
+ application .registerActivityLifecycleCallbacks (
107
107
new Application .ActivityLifecycleCallbacks () {
108
108
@ Override
109
- public void onActivityCreated (@ NonNull Activity activity , Bundle savedInstanceState ) {}
109
+ public void onActivityCreated (@ NonNull Activity activity , Bundle savedInstanceState ) {
110
+ if (inBackground .compareAndSet (true , false )) {
111
+ raiseForegroundNotification ();
112
+ }
113
+ }
110
114
111
115
@ Override
112
- public void onActivityStarted (@ NonNull Activity activity ) {}
116
+ public void onActivityStarted (@ NonNull Activity activity ) {
117
+ if (inBackground .compareAndSet (true , false )) {
118
+ raiseForegroundNotification ();
119
+ }
120
+ }
113
121
114
122
@ Override
115
123
public void onActivityResumed (@ NonNull Activity activity ) {
@@ -132,7 +140,7 @@ public void onActivitySaveInstanceState(
132
140
public void onActivityDestroyed (@ NonNull Activity activity ) {}
133
141
});
134
142
135
- applicationContext .registerComponentCallbacks (
143
+ application .registerComponentCallbacks (
136
144
new ComponentCallbacks2 () {
137
145
@ Override
138
146
public void onTrimMemory (int level ) {
0 commit comments