@@ -352,7 +352,12 @@ public void run() {
352
352
353
353
application .registerActivityLifecycleCallbacks (activityLifecycleCallback );
354
354
if (useNewLifecycleMethods ) {
355
- lifecycle .addObserver (activityLifecycleCallback );
355
+ // NOTE: addObserver is required to run on UI thread,
356
+ // though we made it compatible to run from background thread,
357
+ // there is a chance that lifecycle events get lost if init
358
+ // analytics from background (i.e. analytics is init, but
359
+ // lifecycle hook is yet to be registered.
360
+ run (() -> lifecycle .addObserver (activityLifecycleCallback ));
356
361
}
357
362
}
358
363
@@ -799,6 +804,15 @@ public void run() {
799
804
});
800
805
}
801
806
807
+ private void run (Runnable runnable ) {
808
+ if (Looper .myLooper () == Looper .getMainLooper ()) {
809
+ runnable .run ();
810
+ }
811
+ else {
812
+ HANDLER .post (runnable );
813
+ }
814
+ }
815
+
802
816
/**
803
817
* Asynchronously flushes all messages in the queue to the server, and tells bundled
804
818
* integrations to do the same.
@@ -1000,7 +1014,7 @@ public void shutdown() {
1000
1014
application .unregisterActivityLifecycleCallbacks (activityLifecycleCallback );
1001
1015
if (useNewLifecycleMethods ) {
1002
1016
// only unregister if feature is enabled
1003
- lifecycle .removeObserver (activityLifecycleCallback );
1017
+ run (() -> lifecycle .removeObserver (activityLifecycleCallback ) );
1004
1018
}
1005
1019
// Only supplied by us for testing, so it's ok to shut it down. If we were to make this
1006
1020
// public,
0 commit comments