Android 前后台切换监听

class AppLifeObserver: LifecycleObserver {

    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    fun onForeground() {
        LogUtils.logd("Application enters the foreground")
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    fun onBackground() {
        LogUtils.logd("Application enters the background")
    }
}
class AppLifeObserver: LifecycleEventObserver {
override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
when (event){
Lifecycle.Event.ON_RESUME -> {

}
else -> {}
}
}
}

再Application中调用

lifecycle.addObserver(AppLifeObserver())

 

posted @ 2021-12-14 15:02  勤奋的小铁  阅读(383)  评论(1编辑  收藏  举报