Monday, December 10, 2012

ActivityLifecycleCallbacks and Activity: Callback calling order

The documentation on ActivityLifecycleCallbacks is rather sparse, at best. It seems easy enough to use, but it's not obvious when the ActivityLifecycleCallbacks's functions are called in relation to the Activity's functions. I did a little test:

// ...
public void onActivityResumed(Activity activity) {
    android.util.Log.w("test", "ActivityLifecycleCallbacks.onActivityResumed()");

public void onActivityPaused(Activity activity) {
    android.util.Log.w("test", "ActivityLifecycleCallbacks.onActivityPaused()");
// ...

// ...
protected void onResume() {
    android.util.Log.w("test", "Activity.onResumed() (before super)");
    android.util.Log.w("test", "Activity.onResumed() (after super)");
protected void onPause() {
    android.util.Log.w("test", "Activity.onPaused() (before super)");
    android.util.Log.w("test", "Activity.onPaused() (after super)");
// ...

And the output:
12-10 13:58:35.699: W/test(9794): Activity.onResumed() (before super)
12-10 13:58:35.699: W/test(9794): ActivityLifecycleCallbacks.onActivityResumed()
12-10 13:58:35.709: W/test(9794): Activity.onResumed() (after super)
12-10 13:58:35.749: W/test(9794): Activity.onPaused() (before super)
12-10 13:58:35.749: W/test(9794): ActivityLifecycleCallbacks.onActivityPaused()
12-10 13:58:35.749: W/test(9794): Activity.onPaused() (after super)

Now for the curious, it looks like ActivityLifecycleCallbacks's functions are called as part of the super call chain. So you can expect ActivityLifecycleCallbacks's callbacks to have fired by the time the Activity's super calls finish. Good to know!

1 comment:

  1. Good to know indeed. Thanks for sharing this and saving me the time to test on my own.


Note: Only a member of this blog may post a comment.