Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

People also ask

How do I set parent activity?

Declare a Parent Activity You can do this in the app manifest, by setting an android:parentActivityName attribute. The android:parentActivityName attribute was introduced in Android 4.1 (API level 16). To support devices with older versions of Android, define a <meta-data> name-value pair, where the name is "android.

How do you override a button?

Just call: getSupportActionBar(). setDisplayHomeAsUpEnabled(true); getSupportActionBar(). setHomeButtonEnabled(true);

How do you add action items to the action bar in Android?

All action buttons and other items available in the action overflow are defined in an XML menu resource. To add actions to the action bar, create a new XML file in your project's res/menu/ directory. The app:showAsAction attribute specifies whether the action should be shown as a button on the app bar.


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?


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.


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>