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")
    }
}

再Application中调用

ProcessLifecycleOwner.get().lifecycle.addObserver(AppLifeObserver())