“Back button” using getSupportActionbar and appcompat v7 toolbar

前端 未结 8 592
时光取名叫无心
时光取名叫无心 2021-01-30 02:27

I\'m using the new toolbar from the Appcompat V7 library and I\'m making an application with navigation drawer and with fragments.

In some fragments I don\'t want to sho

相关标签:
8条回答
  • 2021-01-30 02:50

    Add setDisplayHomeAsUpEnabled(true)

        Toolbar toolbar  = findViewById(R.id.toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
    

    Handle the back button

       public boolean onSupportNavigateUp() {
        onBackPressed();
        return true;
    }
    
    0 讨论(0)
  • 2021-01-30 02:52

    Simply you can set Navigation icon and make sure you are setting setNavigationOnClickListener() after setting setSupportActionBar(toolbar)

    toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_arrow_back));
    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onBackPressed();
        }
    });
    
    0 讨论(0)
  • 2021-01-30 02:55

    Not sure if this works in OP's case, but in many cases this is probably the simplest option to implement Back button with the AppCompat Toolbar.

    Skip all the setHomeButtonEnabled, setDisplayHomeAsUpEnabled and onOptionsItemSelected stuff, and related issues.

    Instead, when initialising the Toolbar, simply set 1) navigation icon and 2) navigation OnClickListener for it:

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    
    if (enableBackNavigation) {
        toolbar.setNavigationIcon(R.drawable.ic_back);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }
    
    0 讨论(0)
  • 2021-01-30 02:57

    You can do it like this:

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);  
        toolbar = (Toolbar)findViewById(R.id.toolbar);
        if (toolbar != null) {
          setSupportActionBar(toolbar);
          getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }
    
        setUpNavigationDrawer();
    
        getFragmentManager().addOnBackStackChangedListener(backStackListener); // listen to the backstack of the fragment manager
    }
    

    Define the onBackSTackChangedListener:

    private FragmentManager.OnBackStackChangedListener backStackListener = new FragmentManager.OnBackStackChangedListener() {
       @Override
       public void onBackStackChanged() {
           setNavIcon();
       };
    }
    

    Set the icon according to your fragment's backstack:

    protected void setNavIcon() {
        int backStackEntryCount = getFragmentManager().getBackStackEntryCount();
        drawerToggle.setDrawerIndicatorEnabled(backStackEntryCount == 0);
    }
    

    Detect when the drawer icon is pressed:

    public boolean onOptionsItemSelected(MenuItem item) {
        if (drawerToggle.isDrawerIndicatorEnabled() && drawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
    
        switch (item.getItemId()) {
          case x:
             return true;
          default:
             return false;
        }
    }
    

    And handle the up button:

    public boolean onSupportNavigateUp() {
        onBackPressed();
        return true;
    }
    

    This works for me. Good luck.

    0 讨论(0)
  • 2021-01-30 02:58

    Add this method in onCreate():

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    

    Then override the onOptionItemSelected() as below:

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
    
    0 讨论(0)
  • 2021-01-30 03:02

    activate the back button:

    getActionBar().setDisplayHomeAsUpEnabled(enable);
    

    and listen for clicks in onBackPressed()

    Obviously your activity must extend ActionBarActivity

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