What is Activitylifecyclecallbacks?
- Application provides a set of callback methods through this interface that allows developers to centralize the life cycle events of the activity.
Why use Activitylifecyclecallbacks?
- You might have done this in the past if you had to monitor the activity's lifecycle event code. Rewrite the Onresume () for each acivity, and then make 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. Unified 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
- Define yourself in Androidmanifest application
<application android:name= ". Global. Baseapplication "
Override the OnCreate () method of the application, or call the Application.registeractivitylifecyclecallbacks () method within the application's non-participating constructor 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"); } });};
Execution 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
How to expand and use Activitylifecyclecallbacks
This discussion only attempts to use the log Log tool for a brief test. If you need to meet more complex statistical or debugging requirements. This method can significantly reduce the amount of code inserted and increase efficiency
API only provides this interface callback on the 14+ version number,how does Android 4.0 work under the system?
- The API only leaves an interface callback for the life-cycle events of the above activity, and may not be able to meet today's demands for an increasingly transition to using fragment. How to apply lifecyclecallbacks in a wider range?
On Android Development-activitylifecyclecallbacks usage