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?
I did my own implementation of
Application.ActivityLifecycleCallbacks. I’m usingSherlockActivity, but for normal Activity class might work.First, I’m creating an interface that have all methods for track the activities lifecycle:
Second, I implemented this interface in my Application’s class:
Third, I’m creating a class that extends from SherlockActivity:
Fourth, all class that extend from SherlockActivity, I replaced for MySherlockActivity:
Now, in the logcat you will see the logs programmed in the Interface implementation made in MyApplication.
UPDATE
This implementation was tested from API Level 9 (Gingerbread), API Level 12 (Honeycomb) and API Level 17 (Jelly Bean) and works fine. Might works in Android’s older versions.