Are Application.ActivityLifecycleCallbacks synchronously called before the lifecycle methods in the Activity?

1.5k views Asked by At

I've inherited some nice looking code that uses Application.ActivityLifecycleCallbacks. We're using the onActivityResumed method to take users back to a login screen when their session has timed out.

There's no documentation in the API docs about how this works. Do we get any guarantees about when these methods are called (i.e. are they called before the corresponding onResume in the Activity), and whether they are synchronous with the normal life cycle methods?

If I had to guess, I'd assume these happen in parallel, which means individual activities/fragments would still need to safely cope with being logged out.

2

There are 2 answers

0
David Wasser On BEST ANSWER

These are called on the main (UI) thread and they are called serially (not in parallel). These callbacks are made in the Activity class, so they are made when your activity calls super.onXXXXX();

As an example, see getApplication().dispatchActivityResumed(this); in Activity.onResume() in the Android source code.

0
Marcin Orlowski On

method to take users back to a login screen when their session has timed out.

You can stick to ordinary onResume() for the above task, especially mentioned callbacks are API14+ only