简体   繁体   English

发生onStop时完全终止活动

[英]Completely terminate activity when onStop happens

As per the Activity lifecycle onCreate happens at the first stage, and in my app I execute some vital code in onCreate. 根据活动生命周期,onCreate发生在第一阶段,在我的应用中,我在onCreate中执行了一些重要的代码。 Problem is if I hit the home button or put the app in background the activity will not be recreated via onCreate, but through onResume. 问题是,如果我按下主页按钮或将应用置于后台,则不会通过onCreate而是通过onResume重新创建活动。

I want that once the onStop method is called the app kills itself so that at the next launch it begins from onCreate. 我希望一旦调用onStop方法,该应用程序就会自行终止,以便在下次启动时从onCreate开始。 If it helps my app is made of only one activity. 如果有帮助,我的应用程序仅由一项活动组成。

(The reason I need this is that copying the necessary code in onResume creates bugs) (我需要这样做的原因是,在onResume中复制必要的代码会产生错误)

You can find a manifest flag android:noHistory for the activity which provides this behavior. 您可以找到提供此行为的活动的清单标志android:noHistory

Whether or not the activity should be removed from the activity stack and finished (its finish() method called) when the user navigates away from it and it's no longer visible on screen — "true" if it should be finished, and "false" if not. 当用户离开该活动并且在屏幕上不再可见时,是否应该从活动堆栈中删除该活动并完成(调用了finish()方法)-如果应结束,则为"true" ,否则为"false"如果不。 The default value is "false" . 默认值为"false"

A value of "true" means that the activity will not leave a historical trace. 值为"true"表示该活动将不会留下历史痕迹。 It will not remain in the activity stack for the task, so the user will not be able to return to it. 它不会保留在任务的活动堆栈中,因此用户将无法返回到该任务。 In this case, onActivityResult() is never called if you start another activity for a result from this activity. 在这种情况下,如果您为该活动的结果启动另一个活动,则永远不会调用onActivityResult()

This might be a little bit more secure than using the life-cycle events for it. 这可能比为其使用生命周期事件更安全。 But keep in mind that any kind of state will be lost afterwards. 但是请记住,此后任何状态都会丢失。

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

相关问题 Android活动生命周期-在onStop()之后和调用onDestory()时会发生什么 - Android activity lifecycle - what happens after onStop() and when onDestory() is called 为什么即使我的活动完全不可见,我的活动的onStop()也不被调用,仅onPause()被调用? - Why my activity's onStop() is not getting called only onPause() is getting called even when my activity is completely not visible? IllegalStateException:调用onStop()方法时没有活动 - IllegalStateException: No activity when onStop() method called 重新启动活动时调用onStop延迟 - onStop delays to be called when restarting activity 当对话框显示为活动时,OnStop()vs OnPause() - OnStop() vs OnPause() when dialog is displayed as a activity RecyclerView在触发activity onStop时释放MediaPlayer - RecyclerView release MediaPlayer when activity onStop is triggered 活动处于onStop状态时替换片段 - Replace fragment when activity is onStop state 在调用startActivity时,在onStop()中查找下一个Activity - Find next Activity in onStop() when call startActivity 启动另一个全屏活动时未调用Activity的onStop() - Activity's onStop() not called when starting another full screen Activity 创建子活动时如何避免父活动进入onStop()? - how to avoid parent activity going to onStop() when child activity is created?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM