Returning from an activity using navigateUpFromSameTask()

If you want your application to react this way, you should declare the launch mode of your activity A as:

android:launchMode="singleTop"

in your AndroidManifest.xml.

Otherwise android uses standard launch mode, which means

“The system always creates a new instance of the activity in the
target task”

and your activity is recreated (see Android documentation).

With singleTop the system returns to your existing activity (with the original extra), if it is on the top of the back stack of the task. There is no need to implement onSaveInstanceState in this situation.

savedInstanceState is null in your case, because your activity was not previously being shut down by the OS.

Notice (thanks, android developer for pointing to this):

While this is a solution to the question, it will not work if the activity one returns to is not on the top of the back stack.
Consider the case of activity A starting B, which is starting C, and C’s parent activity is configured to be A.
If you call NavigateUpFromSameTask() from C, the activity will be recreated, because A is not on top of the stack.

In this case one can use this piece of code instead:

Intent intent = NavUtils.getParentActivityIntent(this); 
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP); 
NavUtils.navigateUpTo(this, intent);

Leave a Comment