I have a MainActivity and inside it, I am loading a fragment A. From FragmentA , I am calling google placepicker activity using startActivityforResult as follows.
It seems quite unusual for Android to clean up an activity in the way you described, but if that was the case then your activity should still be restored. Android should not destroy the activity, unless you specifically call finish()
or something causes the activity to end prematurely.
If you refer to the activity lifecycle diagram:
In the scenario you described the first activity should call onStop, but not onDestroy, then when you return from the second activity it should call onStart again.
I created a very simple app to test the scenario you described, which contained the following:
startActivityForResult()
ActivityLifecycleCallbacks
in a custom application classonActivityResult
additionally outputs to the log when it gets calledHere is what is output:
Application is started (FirstActivity is created and started and visible):
FirstActivity onCreate
FirstActivity onStart
FirstActivity onResume
I press the button to start SecondActivity:
FirstActivity onPause
SecondActivity onCreate
SecondActivity onStart
SecondActivity onResume
FirstActivity onSaveInstanceState
FirstActivity onStop
Note, onDestroy does not get called.
Now I press the back button and return to the first activity:
SecondActivity onPause
FirstActivity onStart
FirstActivity onActivityResult
FirstActivity onResume
SecondActivity onStop
SecondActivity onDestroy
The back button calls finish
on SecondActivity so it's destroyed
Now if I press back again FirstActivity will also be finished, causing onDestroy
to be called.
FirstActivity onPause
FirstActivity onStop
FirstActivity onDestroy
You can see that this example has adhered to the lifecycle diagram exactly. The activities are only destroyed once the back button is pressed, which causes the activity to call finish()
.
You mentioned that you tried turning on "Don't keep activities" in the developer options, we can repeat the above experiment which this option enabled and see what happens. I have just added the relevant lifecycle events to save repeating everything that is above:
After pressing the button in the first activity to start the second activity:
...
SecondActivity onResume
FirstActivity onSaveInstanceState
FirstActivity onStop
FirstActivity onDestroy
As expected, the activity has been destroyed this time. This is what happens when you navigate back to the first activity again:
SecondActivity onPause
FirstActivity onCreate
FirstActivity onStart
FirstActivity onActivityResult
FirstActivity onResume
...
This time onCreate was called again as the system didn't have a stopped version of the first activity to restart. Also onActivityResult()
was still called, regardless of the fact that the activity had to be recreated.
This further supports that something in your first activity must be calling finish()
or causing it to crash. However, without seeing your actual code this is conjecture.
Finally, to maintain state if your activity does for some reason need to get recreated, you can override onSaveInstanceState()
and add any state information to the bundle:
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString(MY_STRING_KEY, "my string value");
}
When the activity is recreated, you'll get a bundle back in onCreate which should contain everything you saved:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
if (savedInstanceState != null) {
// Restore previous state
}
}