registerActivityLifecycleCallbacks是Application的一个接口,注册之后应用里的所有activity的生命周期都会被监控起来,我们可以在
此接口方法里实现一些特殊的需求。比如统计用户对每个Activity使用情况,我们可以定义一个BaseActivity,在onStart()和onStop()人工插入统计方法。比如从桌面进应用的时候,检测某些状态,就可以在onActivityResumed方法里检测。
使用方法:在Application 的onCreate方法中使用registerActivityLifecycleCallbacks(callback)注册你的CallBack,如下:
registerActivityLifecycleCallbacks(object:ActivityLifecycleCallbacks{
overridefunonActivityPaused(p0: Activity?) {
}
overridefunonActivityResumed(p0: Activity?) {
}
overridefunonActivityStarted(p0: Activity?) {
}
overridefunonActivityDestroyed(p0: Activity?) {
}
overridefunonActivitySaveInstanceState(p0: Activity?, p1: Bundle?) {
}
overridefunonActivityStopped(p0: Activity?) {
}
overridefunonActivityCreated(p0: Activity?, p1: Bundle?) {
}
})
官方文档:
http://developer.android.com/reference/android/app/Application.ActivityLifecycleCallbacks.html