Android provides us with a pretty decent number of callbacks to handle the UI changes in our app, be it activities or fragments. In fact in case of fragments, they are so much such that making a flowchart out of them can actually scare any developer (See this).
Activities on the other hand provide a very reasonable no of callbacks and each one of them seems to have a separate use case.
Most people already know about the callbacks that the activities provide at their individual level, but what they don't know about is this : Application.ActivityLifecycleCallbacks.
Neither the android developer docs say anything about this interface nor they are referenced from somewhere else in the docs.
Use case of this interface is pretty simple :
If you want to do something after a particular callback (a UI state change) for each activity, you shouldn't be going to every activity that you have and override that method. A very basic example of this which comes to my mind when talking about this is reporting analytics. Want to report the start and stop of each activity in your app? You needn't even touch the activity class for this ! The methods in this interface are called for every activity.
Just register using interface implementation using 'registerActivityLifecycleCallbacks' in your Application class, and you are good to go !
Activities on the other hand provide a very reasonable no of callbacks and each one of them seems to have a separate use case.
Most people already know about the callbacks that the activities provide at their individual level, but what they don't know about is this : Application.ActivityLifecycleCallbacks.
Neither the android developer docs say anything about this interface nor they are referenced from somewhere else in the docs.
Use case of this interface is pretty simple :
If you want to do something after a particular callback (a UI state change) for each activity, you shouldn't be going to every activity that you have and override that method. A very basic example of this which comes to my mind when talking about this is reporting analytics. Want to report the start and stop of each activity in your app? You needn't even touch the activity class for this ! The methods in this interface are called for every activity.
Just register using interface implementation using 'registerActivityLifecycleCallbacks' in your Application class, and you are good to go !
Comments
Post a Comment