Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
468 views
in Technique[技术] by (71.8m points)

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

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...