Automatically log Android lifecycle events using ActivityLifecycleCallbacks?

AWT picture AWT · May 15, 2012 · Viewed 40.3k times · Source

I am trying to automatically capture and log Android lifecycle events using ActivityLifecycleCallbacks, however documentation on this matter is scarce, to say the least:

    public void registerActivityLifecycleCallbacks (Application.ActivityLifecycleCallbacks callback)

I don't want to have to extend the Activity class or override the existing lifecycle methods (onCreate, onResume, etc...) I'm looking to have a separate class listening for these events and acting accordingly.

Does anyone have any experience in this, or have links to good solid documentation or tutorials on how this works? Specifically, how to register for ActivityLifecycleCallbacks, and how to handle them?

Answer

Jeroen picture Jeroen · Jun 18, 2012

I don't have any firsthand experience but judging from the API you can just write your own class that implements the Application.ActivityLifecycleCallbacks interface and register that class on the provided Application class instance

getApplicaton().registerActivityLifecycleCallbacks(yourCustomClass);

This class will receive the same callbacks as your individual activities. Good luck.

PS. This is API level 14 btw, so it won't work on older phones.