android - Activity 自行完成,但对此 Activity 的 onDestroy() 调用发生得太晚了

标签 android android-lifecycle

我在我们的代码中发现了一个错误,我们正在通过

启动 Activity (我们称其为 SCREEN_ACTIVITY)
Intent intent = new Intent(SharedIntents.INTENT_SCREEN);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);

Android 文档:使用 FLAG_ACTIVITY_NEW_TASK 时,如果您正在启动的 Activity 的任务已经在运行,则不会启动新的 Activity ;相反,当前任务将以其上次的状态简单地带到屏幕前面。

所以在大多数情况下一切正常。

当用户单击某些内容时,该屏幕 Activity 将调用 finish()。此屏幕 Activity 也将通过来自服务的传入事件启动或创建。

6053 I/ScreenActivity(15395): onCreate
6682 E/ScreenActivity(15395): finish() Activity
6832 I/ScreenActivity(15395): onDestroy

7284 I/ScreenActivity(15395): onCreate
7911 E/ScreenActivity(15395): finish() Activity
8063 I/ScreenActivity(15395): onDestroy

10555 I/ScreenActivity(15395): onCreate
13413 E/ScreenActivity(15395): finish() Activity
13701 I/ScreenActivity(15395): onCreate
13799 I/ScreenActivity(15395): onDestroy

最后一个是问题。 ScreenActivity 被创建,然后调用 finish() 本身。但问题是 onDestroy 被调用的时间很晚。在此之前,有来自服务器的传入事件并调用 startActivity() 触发新的 ScreenAcitivty onCreate()。问题是 ScreenManager 类保留了创建和销毁该 Activity 的标志。

该标志在 onCreate() 回调和 onDestroy() 回调时设置。 因此,对于第 10555 行和第 13701 行,ScreenManager 设置 createFlag = true。对于第 13799 行,设置 createFlag = false。 13799 行之后的事件将假定未创建 Activity ,并且不会将事件通知给 Activity 。

希望我对问题的描述对您来说很清楚。 这种场景onStop() 和onDestroy() 在调用finish() 之后这么晚才调用预计会发生吗?如果是,我必须考虑解决方案。 如果没有,那有什么地方可以修改吗?

非常感谢。

最佳答案

onDestroy() 可能会或可能不会被调用。你不能依赖它一直被调用。来自docs :

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.

如果您需要知道您的Activity 正在结束,了解它是否正在结束的可靠方法是isFinishing()。方法。您可以在 onPause() 中调用它,以了解此暂停是否最终会导致此 Activity 被销毁:

@Override
protected void onPause() {
    super.onPause();
    if (isFinishing()) {
        // Here  you can be sure the Activity will be destroyed eventually
    }
}

关于android - Activity 自行完成,但对此 Activity 的 onDestroy() 调用发生得太晚了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47430147/

相关文章:

java - Android JSON 解析问题

android - 如何让一个线程与另一个 Activity 线程通信

安卓应用 : onResume() and onStart()

Android - 通过 Intent.FLAG_ACTIVITY_CLEAR_TOP 通过保持状态返回到特定 Activity

java - Android - 全局知道应用程序何时进入后台/用户退出

java - 如何以编程方式更改 RecyclerView 项目属性,特别是 recyclerview 项目内的自定义 View

android - ProGuard:使用 Moshi+Retrofit 的 ClassCastException

android - 如何使用 ItemTouchHelper 及其 Callback 或实现自定义的?

点击主页按钮后 Android 应用程序崩溃

android - 调用方 Activity 的 ondestroy() 是否总是在被调用 Activity 的 oncreate() 之后被调用,还是有异常(exception)?