简体   繁体   中英

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. 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.

I want that once the onStop method is called the app kills itself so that at the next launch it begins from 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)

You can find a manifest flag android:noHistory for the activity which provides this behavior.

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. The default value is "false" .

A value of "true" means that the activity will not leave a historical trace. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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