Android lollipop toolbar switch between open/close drawer and back button

家住魔仙堡 提交于 2019-11-30 19:34:56

From the docs:

To allow Up navigation with the app icon in the action bar, call setDisplayHomeAsUpEnabled():

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

This adds a left-facing caret alongside the app icon and enables it as an action button such that when the user presses it, your activity receives a call to onOptionsItemSelected(). The ID for the action is android.R.id.home.

This means that you will have to implement your back routine on onOptionsItemSelected and check for R.id.home. To avoid calling the routine when you click on the hamburger menu check for canback too on onOptionsItemSelected.

http://developer.android.com/training/implementing-navigation/ancestral.html#up

EDIT

To archieve what you want you will have to implement your own navigation routine.

    mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(shouldBack()) {
               //call onbackpressed or something
                if(displayBackAgain)
                    return; //return after so you don't call syncState();       
            }else if (mNavigationDrawerFragment.isDrawerOpen())
                mNavigationDrawerFragment.closeDrawer();
            else
                mNavigationDrawerFragment.openDrawer();
            mNavigationDrawerFragment.getActionBarDrawerToggle().syncState();
        }
    });
}

To enable the backbutton icon just call getSupportActionBar().setDisplayHomeAsUpEnabled(true); to disable it just call mNavigationDrawerFragment.getActionBarDrawerToggle().syncState();

I found a way to control the back button and the nav. It worked with me. First , set up:

private void setupNav () {

    this.toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(this.toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    this.mActionBarDrawerToggle = new ActionBarDrawerToggle(this, this.mDrawerLayout, this.toolbar, 0, 0);

    this.mActionBarDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //catch back button here.
        }
    });
    this.mDrawerLayout.setDrawerListener(this.mActionBarDrawerToggle);
    this.mActionBarDrawerToggle.syncState();
}

Important thing, this is the way I hide the hamburger and show the back button. You have to put this code in the place you want to show back button. I also lock the Nav when showing back button.

if (!isShowBackButton) {
        mActionBarDrawerToggle.setDrawerIndicatorEnabled(true);
        mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
    } else {

        mActionBarDrawerToggle.setDrawerIndicatorEnabled(false);
        mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
        //enable back button
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!