ActionBar up navigation recreates parent activity instead of onResume

AndroidAndroid Actionbar

Android Problem Overview


I'm using the recommended approach for Up Navigation and my code looks like this:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
	switch (item.getItemId()) {
		case android.R.id.home:
			Intent h = new Intent(ShowDetailsActivity.this, MainActivity.class);
			h.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
			startActivity(h);
			return true;
		default: return super.onOptionsItemSelected(item);
	}
}

Here's the use-case:

  1. I launch my app which is "MainActivity"
  2. I click a button to go to "ShowDetailsActivity"
  3. I click on the UP ActionBar navigation

The issue is after I click on UP, MainActivity hits its onCreate() methods all over again and loses all state instead of starting at the typical onResume() like it would if I just called "finish()" from ShowDetailsActivity. Why? Is this how it always works and this is expected behavior for Android to recreate all activities that are navigated to using the "Up" navigation approach? If I hit the back button I get the expected onResume lifecycle.

This is my solution if an Android "proper" ones doesn't exist:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
	switch (item.getItemId()) {
		case android.R.id.home:
			Intent upIntent = new Intent(this, MainActivity.class);
			if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
				NavUtils.navigateUpTo(this, upIntent);
				finish();
			} else {
				finish();
			}
			return true;
		default: return super.onOptionsItemSelected(item);
	}
}

Android Solutions


Solution 1 - Android

Add the following to your parent activity in the manifest file

android:launchMode="singleTop"

regarding to this answer

Solution 2 - Android

The reason, the activities are recreated when using up-navigation is, that android uses standard launch mode for this, if you do not specify an other mode. That means

> "The system always creates a new instance of the activity in the > target task"

and thus the activity is recreated (see docu here).

A solution would be to either declare the launch mode of the MainActivity as

android:launchMode="singleTop"

in the AndroidManifest.xml
(should always work together with Intent.FLAG_ACTIVITY_CLEAR_TOP)

or you could add FLAG_ACTIVITY_SINGLE_TOP to your intent flags, to tell the activity, that it should not be recreated, if it is on the back stack, e.g.

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

Solution 3 - Android

This code worked for me:

Intent upIntent = NavUtils.getParentActivityIntent(this);
if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
    // This activity is NOT part of this app's task, so create a new task
    // when navigating up, with a synthesized back stack.
    TaskStackBuilder.create(this)
        // Add all of this activity's parents to the back stack
        .addNextIntentWithParentStack(upIntent)
        // Navigate up to the closest parent
        .startActivities();
} else {
    // This activity is part of this app's task, so simply
    // navigate up to the logical parent activity.
    upIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    NavUtils.navigateUpTo(this, upIntent);
}

return true;

No finish() invocation required.

Solution 4 - Android

Beginning in Android 4.1 (API level 16), you can declare the logical parent of each activity by specifying the android:parentActivityName attribute in the element. If your app supports Android 4.0 and lower, include the Support Library with your app and add a element inside the . Then specify the parent activity as the value for android.support.PARENT_ACTIVITY, matching the android:parentActivityName attribute.

<application ... >
...
<!-- The main/home activity (it has no parent activity) -->
<activity
    android:name="com.example.myfirstapp.MainActivity" ...>
    ...
</activity>
<!-- A child of the main activity -->
<activity
    android:name="com.example.myfirstapp.DisplayMessageActivity"
    android:label="@string/title_activity_display_message"
    android:parentActivityName="com.example.myfirstapp.MainActivity" >
    <!-- Parent activity meta-data to support 4.0 and lower -->
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value="com.example.myfirstapp.MainActivity" />
</activity>

If the parent activity has launch mode , or the up intent contains FLAG_ACTIVITY_CLEAR_TOP, the parent activity is brought to the top of the stack, and receives the intent through its onNewIntent() method.

<activity
        android:launchMode="singleTop"
        android:name="com.example.myfirstapp.MainActivity">

    </activity>

In Above Code "SingleTop" ,a new instance of a "singleTop" activity may also be created to handle a new intent. However, if the target task already has an existing instance of the activity at the top of its stack, that instance will receive the new intent (in an onNewIntent() call); a new instance is not created.

For Detail Documentation Click Here

To navigate up when the user presses the app icon, you can use the NavUtils class's static method, navigateUpFromSameTask(). For That Read The Documentaion given in the Link Above.

Solution 5 - Android

What works for me (and I also think the cleanest) is to reorder the activity to front. If it doesn't exist on the stack it will be created as you 'nav up'.

Intent upIntent = NavUtils.getParentActivityIntent(this);
upIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(upIntent);
finish();
return true;
	

Solution 6 - Android

you just need to go back , not to create the activity again.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            super.onBackPressed();
            return true;
    }
    return super.onOptionsItemSelected(item);
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser123321View Question on Stackoverflow
Solution 1 - AndroidCarmenView Answer on Stackoverflow
Solution 2 - AndroidyonojoyView Answer on Stackoverflow
Solution 3 - AndroidVictor DenisovView Answer on Stackoverflow
Solution 4 - Androidkatwal-DipakView Answer on Stackoverflow
Solution 5 - AndroidDavidView Answer on Stackoverflow
Solution 6 - AndroidMoaz RashadView Answer on Stackoverflow