How to implement the Android ActionBar back button?

后端 未结 12 2148
天涯浪人
天涯浪人 2020-11-29 15:38

I have an activity with a listview. When the user click the item, the item \"viewer\" opens:

List1.setOnItemClickListener(new OnItemClickListener() {
    @Ov         


        
相关标签:
12条回答
  • 2020-11-29 16:11

    In the OnCreate method add this:

    if (getSupportActionBar() != null)
        {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }
    

    Then add this method:

    @Override
    public boolean onSupportNavigateUp() {
        onBackPressed();
        return true;
    }
    
    0 讨论(0)
  • 2020-11-29 16:12

    Selvin already posted the right answer. Here, the solution in pretty code:

    public class ServicesViewActivity extends Activity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            // etc...
            getActionBar().setDisplayHomeAsUpEnabled(true);
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
            case android.R.id.home:
                NavUtils.navigateUpFromSameTask(this);
                return true;
            default:
                return super.onOptionsItemSelected(item);
            }
        }
    }
    

    The function NavUtils.navigateUpFromSameTask(this) requires you to define the parent activity in the AndroidManifest.xml file

    <activity android:name="com.example.ServicesViewActivity" >
        <meta-data
         android:name="android.support.PARENT_ACTIVITY"
         android:value="com.example.ParentActivity" />
    </activity>
    

    See here for further reading.

    0 讨论(0)
  • 2020-11-29 16:12

    To enable the ActionBar back button you obviously need an ActionBar in your Activity. This is set by the theme you are using. You can set the theme for your Activity in the AndroidManfiest.xml. If you are using e.g the @android:style/Theme.NoTitleBar theme, you don't have an ActionBar. In this case the call to getActionBar() will return null. So make sure you have an ActionBar first.

    The next step is to set the android:parentActivityName to the activity you want to navigate if you press the back button. This should be done in the AndroidManifest.xml too.

    Now you can enable the back button in the onCreate method of your "child" activity.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getActionBar().setDisplayHomeAsUpEnabled(true);
    }
    

    Now you should implement the logic for the back button. You simply override the onOptionsItemSelected method in your "child" activity and check for the id of the back button which is android.R.id.home.

    Now you can fire the method NavUtils.navigateUpFromSameTask(this); BUT if you don't have specified the android:parentActivityName in you AndroidManifest.xml this will crash your app.

    Sometimes this is what you want because it is reminding you that you forgot "something". So if you want to prevent this, you can check if your activity has a parent using the getParentActivityIntent() method. If this returns null, you don't have specified the parent.

    In this case you can fire the onBackPressed() method that does basically the same as if the user would press the back button on the device. A good implementation that never crashes your app would be:

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                if (getParentActivityIntent() == null) {
                    Log.i(TAG, "You have forgotten to specify the parentActivityName in the AndroidManifest!");
                    onBackPressed();
                } else {
                    NavUtils.navigateUpFromSameTask(this);
                }
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
    

    Please notice that the animation that the user sees is different between NavUtils.navigateUpFromSameTask(this); and onBackPressed().

    It is up to you which road you take, but I found the solution helpful, especially if you use a base class for all of your activities.

    0 讨论(0)
  • 2020-11-29 16:23

    Building on Jared's answer, I had to enable and implement the action bar back button behavior in several activities and created this helper class to reduce code duplication.

    public final class ActionBarHelper {
        public static void enableBackButton(AppCompatActivity context) {
            if(context == null) return;
    
            ActionBar actionBar = context.getSupportActionBar();
            if (actionBar == null) return;
    
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
    }
    

    Usage in an activity:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...
    
        ActionBarHelper.enableBackButton(this);
    }
    
    0 讨论(0)
  • 2020-11-29 16:26

    I think onSupportNavigateUp() is simplest and best way to do so

    check the code in this link Click here for complete code

    0 讨论(0)
  • 2020-11-29 16:27

    If you are using Toolbar, I was facing the same issue. I solved by following these two steps

    1. In the AndroidManifest.xml
    <activity android:name=".activity.SecondActivity" android:parentActivityName=".activity.MainActivity"/>
    
    1. In the SecondActivity, add these...
    Toolbar toolbar = findViewById(R.id.second_toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    
    0 讨论(0)
提交回复
热议问题