gpt4 book ai didi

android - Lifecycle.Event.ON_DESTROY 未在应用程序中调用

转载 作者:行者123 更新时间:2023-12-05 00:18:48 26 4
gpt4 key购买 nike

我想在用户退出应用程序时调用一些方法(请参阅 12 以获得帮助)。

class BaseApplication : Application(), LifecycleObserver {

override fun onCreate() {
super.onCreate()

// Register observer.
ProcessLifecycleOwner.get().lifecycle.addObserver(this)
}

@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
fun init() {
println("*** called onCreate()")
}

@OnLifecycleEvent(Lifecycle.Event.ON_START)
fun LibOnStart() {
println("*** called onStart()")
}

@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
fun LibOnStop() {
println("*** called onStop()")
}

@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
fun LibOnResume() {
println("*** called onResume()")
}

@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
fun LibOnPause() {
println("*** called onPause()")
}

@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
fun cleanup() {
println("*** called onDestroy()")
}
}

ON_DESTROY 外,这些事件都会被触发。

I/System.out: *** called onPause() of Activity
I/System.out: *** called onStop() of Activity

可能 Application 没有从内存中清除。如果我从最近列表中滑动应用程序或强制停止,它不会触发 destroy 事件。也许这些事件在 Activity 中正常工作。如何在 Application 中捕获 destroy 事件?

最佳答案

Documentation声称

You can consider this LifecycleOwner as the composite of all of yourActivities, except that Lifecycle.Event.ON_CREATE will be dispatchedonce and Lifecycle.Event.ON_DESTROY will never be dispatched.

所以没有办法在应用程序中处理onDestroy

或者,您可以尝试使用 onTaskRemoved Service 类的回调,以捕捉应用程序从最近应用程序中删除的时刻。

关于android - Lifecycle.Event.ON_DESTROY 未在应用程序中调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66115576/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com