ActivityLifecycleCallbacks是什么?
Application通过此接口提供了一套回调方法,用于让开发者可以对Activity的生命周期事件进行集中处理。
使用示例:在自己实现的Application的onCreate方法中使用以下代码
//需要API14+
if(Build.VERSION.SDK_INT > 14) {
registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
Log.e("LOG", activity + " onCreated.");
//可以在这里面进行Activity使用情况的统计
}
@Override
public void onActivityStarted(Activity activity) {
}
@Override
public void onActivityResumed(Activity activity) {
}
@Override
public void onActivityPaused(Activity activity) {
}
@Override
public void onActivityStopped(Activity activity) {
}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
}
@Override
public void onActivityDestroyed(Activity activity) {
}
});
打印结果:
01-16 15:27:45.244 17878-17878/com.soarsky.sun E/total: reconstruction.controller.login.WelcomeActivity@42345948 onCreated.