如何完成()与应用程序关联的堆栈上的每个活动?
有没有办法告诉 Android,“如果用户通过按‘home’键退出此 Activity,请立即完成()堆栈上曾经与此应用程序关联的每个 Activity?”或者,另一种方法是,用户按“home”强制调用其 onPause() 方法的 Activity 不仅可以完成其本身,还可以启动启动它的 Activity?
我遇到的问题是工作流程。我的应用程序的用户基本上在主活动和“详细”活动窗口之间来回切换。他们在主 Activity 中看到某些内容,点击它,子详细 Activity 就会生成,他们使用它一段时间,然后回击以返回主 Activity。启动子活动后我无法完成()主活动,因为那样后退按钮将停止工作。问题是,现在,如果用户通过在子 Activity 中按主页键退出,则会留下僵尸父 Activity。如果用户重新启动应用程序,使用它一段时间,然后通过从主 Activity 中点击菜单->退出(我在其中终止服务,然后调用 finish())显式触发关闭,则 Activity 消失,并且用户从上次运行中被转储到僵尸 Activity 中(如果用户尝试执行任何操作,该 Activity 会立即崩溃,因为它所依赖的后台服务已被关闭,并且所有本来正在侦听的 BroadcastReceiver 已被删除),所以它的呼救声消失了闻所未闻)。
我实际上有点困惑,因为我的启动活动的清单标记中已经有 android:launchMode="singleTask" ,并且我认为 launchMode="singleTask" 明确应该通过确保防止此类事情发生在重新启动之前,上一次运行中任何残留的 Activity 都已安全终止。
Is there any way to tell Android, "If the user exits this Activity by hitting the 'home' key, immediately finish() every Activity on the stack that has ever been associated with this Application?" Or, alternatively, a way for an Activity whose onPause() method has been forcibly called by a user hitting 'home' to finish() not only itself, but the Activity that started it as well?
The problem I have is workflow. My application's users basically go back and forth between a main Activity and "detail" Activity windows. They see something in the main Activity, tap it, the child detail Activity gets spawned, they use it for a while, then hit back to return to the main Activity. I can't finish() the main Activity after starting the child Activity, because then the back button would quit working. The problem is that now, if the user exits by hitting the home key in a child Activity, it leaves behind a zombie parent Activity. If the user relaunches the application, uses it for a while, then explicitly triggers a shutdown by hitting menu->quit from the main Activity (where I kill the service, then call finish()), the Activity goes away, and the user gets dumped into the zombie Activity from the previous run (which promptly crashes if the user tries to do anything, because the background service it depends on has already been shut down, and all the BroadcastReceivers that would have otherwise been listening have been removed, so its cries for help go unheard).
I'm actually kind of puzzled, because I already have android:launchMode="singleTask" sitting in the manifest tag for my launch activity, and I thought launchMode="singleTask" was explicitly supposed to prevent things like this from happening by making sure that any lingering Activities from a previous run were safely dead before it relaunched.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最简单的方法是:
The easiest way of doing this is:
也许您可以使用
clearTaskOnLaunch
标记堆栈中的所有活动(请参阅http://developer.android.com/guide/topics/manifest/activity-element.html#clear).您可以标记任务中的所有活动与属性。Perhaps you can mark all the Activities in the stack with
clearTaskOnLaunch
(see http://developer.android.com/guide/topics/manifest/activity-element.html#clear).You can mark all the Activities that would be in the task with the attribute.