简体   繁体   English

startActivityForResult有时会导致主活动结束并从活动堆栈中删除

[英]startActivityForResult sometimes cause main Activity finish and removed from the activities stack

I have a main app A , use startActivityForResult to start app B . 我有一个主app A ,请使用startActivityForResult启动app B

Everything goes fine but recently sometimes app A has been finished and removed from the activity stack (without any error prompt). 一切正常,但最近有时app A已完成并从活动堆栈中删除(没有任何错误提示)。

App B is still running well without any problem. App B仍然运行良好,没有任何问题。

After I check for the activity stack, app A was gone and app B is still there ( adb shell dumpsys activity command). 在我检查了活动堆栈之后, app A消失了,而app B仍然在那里( adb shell dumpsys activity命令)。

Anybody know how to solve this issue? 有人知道如何解决这个问题吗?

In the documentation of the onDestroy() method of Activity. 在Activity的onDestroy()方法的文档中。 It's said : 据说:

Perform any final cleanup before an activity is destroyed. 在销毁活动之前执行任何最后的清理。 This can happen either because the activity is finishing (someone called finish() on it, or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios with the isFinishing() method. 这可能是由于活动即将完成(有人在其上称为finish()),或者因为系统正在临时销毁该活动实例以节省空间。您可以使用isFinishing()方法区分这两种情况。

The system could choose by itself to kill your activity A if it consider it needs more place. 如果系统认为它需要更多位置,则可以自行选择杀死您的活动A。 That's what append to you. 那就是您的附加内容。

As said in the doc, you could avoid it by Override the OnDestroy() method and check if it's finishing by user call or by system call using isFinnishing() . 如文档中所述,您可以通过重写OnDestroy()方法并通过使用isFinnishing()通过用户调用或系统调用检查它是否完成来避免它。 Then you could for example call super.onDestroy() only if it's a user destroy and not a system one. 然后,例如,仅当它是用户销毁而不是系统销毁时,才可以调用super.onDestroy() Be careful this mean the system will never be able to kill your activity by itself so you have to make sure you call finnish() 请注意,这意味着系统将永远无法自行终止您的活动,因此您必须确保调用finnish()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM