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>
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?