54

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>
  • 1
    CLEAR_TOP has nothing to do with the login activity which comes before list activity. It states that everything following the list activity should be finished. Consider having LOGIN, LIST, DETAIL, LIST, DETAIL started. After you start the LIST activity with CLEAR_TOP flag the stack will look as follows : LOGIN, LIST. The LIST activity will get recreated from scratch. If you specify SINGLE_TOP in addition, the LIST activity will instead be preserved and receive onNewIntent(Intent) callback. – Eugen Pechanec Sep 15 '15 at 19:43
  • Possible duplicate of [Android Actionbar Up button versus system Back button](http://stackoverflow.com/questions/10320179/android-actionbar-up-button-versus-system-back-button) – avalancha Oct 09 '15 at 15:10

4 Answers4

106

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: How can I return to a parent activity correctly?

Community
  • 1
  • 1
vikki_logs
  • 1,521
  • 2
  • 10
  • 21
  • 3
    Perfect Answer for Action bar Up issue ... :) . Thanks – Pravin Mohol Sep 06 '14 at 18:24
  • Thanks. This answer help me in my situation because I did not want the parent view to be created. I wanted the child view to be dismissed in the same way as the back button. – Mark.ewd Oct 22 '14 at 04:15
  • 7
    this works great!. does anyone know why this is the default behaviour on android, it really makes no sense. Why keep a hold of an activity on memory, if you are gonna destroy it whenever the user asks for it D: – frankelot May 29 '15 at 00:16
  • I started a bounty for this comment by @feresr. I'm curious as to why this is the behavior. I could have sworn this was not the behavior a few years ago in older api levels. – EGHDK Sep 11 '15 at 00:22
  • Another good reference worth reading to understand the issue: Android Developers Providing Up Navigation: https://developer.android.com/training/implementing-navigation/ancestral.html – Krzysiek Mar 02 '16 at 10:08
35

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.

Aashir
  • 2,621
  • 4
  • 21
  • 37
  • This looks like it does the trick; with this change the soft back button and the actionbar up button now follow the same codepath. –  Mar 04 '14 at 21:45
  • 8
    **A better answer was given by @vikki_logs below**. The back button and 'Up' have different behavior, they shouldn't do the same thing. If I navigated to the detail activity `ShowInstanceActivity` then hitting back should take me back to _my_ application or where I was previously. Using the Up button should navigate me backwards in terms of _your_ application. – MadTom Aug 06 '15 at 13:55
3

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.

Community
  • 1
  • 1
Jolson Da Costa
  • 1,095
  • 1
  • 12
  • 31
  • Up should navigate inside of your application. Finishing the current activity doesn't gurantee that you'll stay inside of the current application. – EGHDK Sep 15 '15 at 14:15
  • Well, it needn't exit your application everytime. it depends what code you write for each activity for 'android.R.id.home'. – Jolson Da Costa Sep 16 '15 at 04:43
1

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>
T3KBAU5
  • 1,861
  • 20
  • 26
  • Thank you for the suggestion, however this didn't work for me and the actionbar up button still destroys the parent activity. –  Mar 04 '14 at 21:44
  • Please add in manifest in parent activity - android:launchMode="singleTop"... This worked for me ..Enjoyed :) – Pravin Mohol Sep 06 '14 at 18:25