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>
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.
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?
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.
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>
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".WelcomeActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity" />
<activity android:name=".StartActivity" />
<activity
android:name=".LoginActivity"
android:parentActivityName=".StartActivity"/>
<activity
android:name=".RegisterActivity"
android:parentActivityName=".StartActivity"/>
<activity android:name=".SettingActivity">
</activity>
</application>
Anyone can check this code and find error??/
When i click on back button app is automatically closed...