What is Activitylifecyclecallbacks?
- The application provides a set of callback methods that allow developers to centralize the activity's lifecycle events.
Why use Activitylifecyclecallbacks?
- In the past, if you were to monitor the activity's life cycle event code, you might have done so by rewriting each acivity Onresume () and then doing statistics and processing:
@Overrideprotected void Onresume () { super.onresume (); TODO Processing and Statistics code LOG.V (TAG, "Onresume"); LOGGER.V (TAG, "Onresume"); LOGGING.V (TAG, "Onresume"); ...}
- Activitylifecyclecallbacks interface callbacks can simplify this tedious process and unify processing in a class
How to use Activitylifecyclecallbacks?
- Android.app.Application.ActivityLifecycleCallbacks
- Request API 14+ (Android 4.0+)
- Inherit application
public class Baseapplication extends application
- Custom application in Androidmanifest
<application android:name= ". Global. Baseapplication "
-
Override application's OnCreate () method, call the Application.registeractivitylifecyclecallbacks () method, and implement the Activitylifecyclecallbacks interface
public void OnCreate () {super.oncreate (); This.registeractivitylifecyclecallbacks (New Activitylifecyclecallbacks () {@Override public void onactivitystopped ( Activity activity) {LOGGER.V (activity, "onactivitystopped"); } @Override public void onactivitystarted (activity activity) {LOGGER.V (activity, "onactivitystarted"); } @Override public void onactivitysaveinstancestate (activity activity, Bundle outstate) {logger.v (activity, "Onactivitysaveinstancestate"); } @Override public void onactivityresumed (activity activity) {LOGGER.V (activity, "onactivityresumed"); } @Override public void onactivitypaused (activity activity) {LOGGER.V (activity, "onactivitypaused"); } @Override public void onactivitydestroyed (activity activity) {LOGGER.V (activity, "onactivitydestroyed"); } @Override public void onactivitycreated (activity activity, Bundle savedinstancestate) {LOGGER.V (actiVity, "onactivitycreated"); } });};
-
Run results (logcat log)
10-21 14:32:57.722:v/welcomeactivity (8085): onactivitycreated10-21 14:32:57.762:v/welcomeactivity (8085): Onactivitystarted10-21 14:32:57.762:v/welcomeactivity (8085): onactivityresumed10-21 14:32:59.164:v/ Welcomeactivity (8085): onactivitypaused10-21 14:32:59.194:v/mainactivity (8085): onactivitycreated10-21 14:32:59.224:v/mainactivity (8085): onactivitystarted10-21 14:32:59.224:v/mainactivity (8085): Onactivityresumed10-21 14:32:59.735:v/welcomeactivity (8085): onactivitystopped10-21 14:32:59.735:v/ Welcomeactivity (8085): onactivitydestroyed10-21 14:33:06.502:v/mainactivity (8085): onactivitypaused10-21 14:33:06.612:v/mainactivity (8085): onactivitystopped10-21 14:33:06.612:v/mainactivity (8085): onActivityDestroyed
The extension usage of activitylifecyclecallbacks
-
This approach only attempts to use the Log Log tool for brief testing, which can significantly reduce the amount of code injected and increase efficiency if you need to meet more complex statistical or debugging requirements.
-
The API only provides this interface callback on the 14+ version,How does Android 4.0 use the following system?
- The API only leaves an interface callback for the life-cycle events of the above activity, and may not be able to meet the needs of today's increasingly transitioning to use fragment, how to apply lifecyclecallbacks on a larger scale?
On Android Development-Activitylifecyclecallbacks use method