@Override protected void onResume() { super.onResume(); //TODO 处理和统计代码 Log.v(TAG, "onResume"); Logger.v(TAG, "onResume"); Logging.v(TAG, "onResume"); ... }
public class BaseApplication extends Application
<application android:name=".global.BaseApplication"
重写Application的onCreate()方法,或在Application的无參构造方法内,调用Application.registerActivityLifecycleCallbacks()方法,并实现ActivityLifecycleCallbacks接口
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"); } }); };
执行结果(Logcat日志)
10-21 14:32:57.722: V/WelcomeActivity(8085): onActivityCreated 10-21 14:32:57.762: V/WelcomeActivity(8085): onActivityStarted 10-21 14:32:57.762: V/WelcomeActivity(8085): onActivityResumed 10-21 14:32:59.164: V/WelcomeActivity(8085): onActivityPaused 10-21 14:32:59.194: V/MainActivity(8085): onActivityCreated 10-21 14:32:59.224: V/MainActivity(8085): onActivityStarted 10-21 14:32:59.224: V/MainActivity(8085): onActivityResumed 10-21 14:32:59.735: V/WelcomeActivity(8085): onActivityStopped 10-21 14:32:59.735: V/WelcomeActivity(8085): onActivityDestroyed 10-21 14:33:06.502: V/MainActivity(8085): onActivityPaused 10-21 14:33:06.612: V/MainActivity(8085): onActivityStopped 10-21 14:33:06.612: V/MainActivity(8085): onActivityDestroyed
本次初探仅尝试使用Log日志工具作简要測试。如需满足较复杂的统计或调试需求时。此法可能会大大降低插入代码量,提高效率
API仅在14+版本号上提供此接口回调,Android 4.0下面系统怎样使用?
Android开发 - ActivityLifecycleCallbacks用法初探
原文:http://www.cnblogs.com/lcchuguo/p/5106431.html