gpt4 book ai didi

android - 通过滑动从后台删除 Activity 后,不会调用 Activity 的 onDestroy() 方法

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:49:28 24 4
gpt4 key购买 nike

我在我的 Application 类中的私有(private)类中实现了 ActivityLifecycleCallbacks

如果使用主页按钮或后退按钮,将调用生命周期的 onStop()onPause() 方法。如果我通过从后台滑出来终止 Activity ,则不会调用 onDestroy() 方法。下次我开始 Activity 时会调用它。我正在使用 moveTaskToBack(true) 以防按下。

一定是什么问题,当我们从后台滑出时应该调用哪个生命周期方法?

最佳答案

来自documentation onDestroy() 的:

Note: do not count on this method being called as a place for saving data! For example, if an activity is editing data in a content provider, those edits should be committed in either onPause() or onSaveInstanceState(Bundle), not here. This method is usually implemented to free resources like threads that are associated with an activity, so that a destroyed activity does not leave such things around while the rest of its application is still running. There are situations where the system will simply kill the activity's hosting process without calling this method (or any others) in it, so it should not be used to do things that are intended to remain around after the process goes away.

这种情况是将应用从最近的任务列表中滑出。

查看 this answer 了解当应用从“最近”中删除时如何收到通知。

关于android - 通过滑动从后台删除 Activity 后,不会调用 Activity 的 onDestroy() 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38118107/

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