If you kill your application when it is in the foreground, this is not the same as when Android kills your application (which will only do when your application is in the background). If you kill and then restart the application, it’s the same as starting over from scratch. There is no “recovery." If you add logging to onCreate() , you will see that after you kill and restart the application, the Bundle that is passed to onCreate() is null.
Unfortunately, it’s rather difficult to simulate what happens when Android kills your application.
EDIT: added more stuff after OP comment
Here is a concrete example for discussion. The first without the developer option is "Do not perform actions":
ActivityA is the root activity.- Let's start
ActivityA ActivityA.onCreate() is calledActivityA Now Starts ActivityB CalledActivityB.onCreate() (the activity stack now contains ActivityA ActivityB )ActivityB launches ActivityA using FLAG_ACTIVITY_CLEAR_TOP | FLAG_ACTIVITY_SINGLE_TOP FLAG_ACTIVITY_CLEAR_TOP | FLAG_ACTIVITY_SINGLE_TOP and optional "foo"ActivityA.onNewIntent() is called with an Intent containing FLAG_ACTIVITY_CLEAR_TOP | FLAG_ACTIVITY_SINGLE_TOP FLAG_ACTIVITY_CLEAR_TOP | FLAG_ACTIVITY_SINGLE_TOP and optional "foo"ActivityB.onDestroy() is called because the action stack has been deleted before ActivityA
Now we’ll do the same, but turn on the developer’s option “Do not continue actions” (I highlighted in bold material different from the previous one):
ActivityA is the root activity.- Let's start
ActivityA ActivityA.onCreate() is calledActivityA Now Starts ActivityB CalledActivityB.onCreate() (the activity stack now contains ActivityA ActivityB )- Since
ActivityA stopped, Android destroys it and calls ActivityA.onDestroy() - Note. There is still
ActivityA → ActivityB in the operation stack, although there is currently no instance of ActivityA . Android remembers all state ActivityB launches ActivityA using FLAG_ACTIVITY_CLEAR_TOP | FLAG_ACTIVITY_SINGLE_TOP FLAG_ACTIVITY_CLEAR_TOP | FLAG_ACTIVITY_SINGLE_TOP and optional "foo"- Since Android does not have an
ActivityA instance to re-enable, it needs to create it, so it does, and then ... ActivityA.onCreate() is called with the same Intent with which it was called when the original instance of ActivityA was created (i.e.: LAUNCH intent without flags and no additional functions)ActivityA.onNewIntent() is called with an Intent containing FLAG_ACTIVITY_CLEAR_TOP | FLAG_ACTIVITY_SINGLE_TOP FLAG_ACTIVITY_CLEAR_TOP | FLAG_ACTIVITY_SINGLE_TOP and optional "foo"ActivityB.onDestroy() is called because the action stack has been deleted before ActivityA
It is important to note that Android always calls onCreate() whenever it creates an instance of activity. Think of it as a constructor Activity If Android needs to recreate an Activity instance because the process was killed or the action was destroyed, it will create a new object, then call onCreate() , and then (if necessary) will call onNewIntent() .
When you call setIntent() , it does not actually change the Intent , which Android saves and restores. This only changes the built-in Intent memory that will be returned from the getIntent() call.
I hope this is clearer now. If not, let me know.
David wasser
source share