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

前端 未结 4 624
旧巷少年郎
旧巷少年郎 2020-12-13 01:48

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, whi

相关标签:
4条回答
  • 2020-12-13 02:20

    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?

    0 讨论(0)
  • 2020-12-13 02:23

    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>
    
    0 讨论(0)
  • 2020-12-13 02:29

    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.

    0 讨论(0)
  • 2020-12-13 02:32

    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.

    0 讨论(0)
提交回复
热议问题