ActionBar 'up' button destroys parent activity, 'back' does not

AndroidAndroid ActionbarAndroid Actionbar-Compat

Android Problem Overview


I have a relatively simple Android app with one Activity showing a list of items and another showing details of a selected item. I start the list activity, which is my topmost activity (using FLAG_ACTIVITY_CLEAR_TOP to clear the login activity from which this is called) with:

Intent intent = new Intent(this, ListInstancesActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();

and from within that activity I act on an item being selected with:

Intent detailIntent = new Intent(this, ShowInstanceActivity.class);
detailIntent.putExtra(ShowInstanceFragment.ARG_ITEM_ID, id);
startActivity(detailIntent);

All works fine, and if I use the softkey 'back' button then I return to the ListInstancesActivity as I would expect. However, if instead I press the back/up button on the action bar then it destroys and recreates the ListInstancesActivity. This is bad, as it is relatively computationally expensive to do so.

How can I make the action bar behave in the same way as the softkey and just return to the previous activity rather than destroying it.

It should be noted that I'm using the support library version of the actionbar.

The relevant parts of my AndroidManifest.xml are

<activity
  android:name=".agenda.ListInstancesActivity"
  android:label="@string/list_instances_activity_title">
</activity>
<activity
  android:name=".agenda.ShowInstanceActivity"
  android:label="@string/show_instance_activity_title"
  android:parentActivityName=".agenda.ListInstancesActivity">
</activity>

Android Solutions


Solution 1 - Android

In the android manifest.xml adding the following attribute for the parent activity tag worked for me.

android:launchMode="singleTop"

Reference : http://developer.android.com/guide/topics/manifest/activity-element.html

Refer the similar question: https://stackoverflow.com/questions/12276027/how-can-i-return-to-a-parent-activity-correctly

Solution 2 - Android

You can override what the actionbar up button should do like:

public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {

case android.R.id.home:
    onBackPressed();
    return true;
}

return super.onOptionsItemSelected(item);
}

And recreate the back button effect.

Solution 3 - Android

when you specify parent activity in manifest then it gets restarted when you click on up Navigation button in action bar.

check this i already answered this question

https://stackoverflow.com/a/32401235/3479012

you need to override up nevigation button in actionbar by accessing it by android.R.id.home in onOptionsItemSelected and do finish top activity.

Solution 4 - Android

It looks like your parent activity isn't setup properly in your manifest. Add this inside your ShowInstanceActivity activity tag:

<meta-data android:name="android.support.PARENT_ACTIVITY" android:value=".agenda.ListInstancesActivity" />

So, your final activity tag should look like:

<activity
  android:name=".agenda.ShowInstanceActivity"
  android:label="@string/show_instance_activity_title"
  android:parentActivityName=".agenda.ListInstancesActivity">
  <meta-data android:name="android.support.PARENT_ACTIVITY" android:value=".agenda.ListInstancesActivity" />
</activity>

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
Questionuser1596371View Question on Stackoverflow
Solution 1 - Androidvikki_logsView Answer on Stackoverflow
Solution 2 - AndroidAashirView Answer on Stackoverflow
Solution 3 - AndroidJolson Da CostaView Answer on Stackoverflow
Solution 4 - AndroidT3KBAU5View Answer on Stackoverflow