Is there a way to register for an activity’s events? I’m specifically interested in the onStart / onStop events, and I don’t want to add special operations in the activity for that.
Is there a way to register for an activity’s events? I’m specifically interested in
Share
One way to get events from the lifecycle of other activities is to register your class as an
Application.ActivityLifecycleCallbackswith the mainApplicationinstance and filter events for theActivityyou’re interested in.This is a short example (you may want to register the callbacks from another method/class other than
MainActivity.onCreateor you’ll miss that message 😉 and you may have a dependency there that you don’t want)On the activity you want to spy:
Then the Spy code looks something like:
You can also register the spy from another place if you have a reference to the Activity you want to follow.
I hope this helps 🙂
EDIT: I forgot to mention, this will only work on API Level 14 and above…