0

After reading the following question, I still can't figure out how to replace my Navigation Drawer's menu with a simple back arrow that lets my user go back to the previous Fragment.

So far I'm able to hide the button to access the Drawer like such:

public void setDrawerState(boolean isEnabled) {
    if ( isEnabled ) {
        drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);           
        toggle.setDrawerIndicatorEnabled(true);
        toggle.syncState();

    }
    else {
        drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);           
        toggle.setDrawerIndicatorEnabled(false);
        toggle.syncState();
    }
}

How may I add the back arrow at its place?

Tatsuya Fujisaki
  • 1,434
  • 15
  • 18

1 Answers1

1

It's little late but I found one workaround. I have used following in my activity

Firstly set drawer locked mode

 fullLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);

Then add below code to display back button

ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);

Then create drawer toggle and add drawer listner. Use below code.

 ActionBarDrawerToggle mToggle = new ActionBarDrawerToggle(this, fullLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    mToggle.setDrawerIndicatorEnabled(false);
    mToggle.syncState();
    mToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //Backpress action
            finish();
        }
    });
    fullLayout.setDrawerListener(mToggle);

Note - method setDrawerListener is deprecated. Modify above code as per your need.

pratik
  • 211
  • 2
  • 8
  • can you tell how to restore drawer button and remove that lock mode ? – JenuRudan May 20 '18 at 20:18
  • you can try setting fullLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED); and actionBar.setDisplayHomeAsUpEnabled(false); I have not tried this code but i think it should work. – pratik May 22 '18 at 09:10
  • tried it but the back button disappears but the drawer button doesn't show back – JenuRudan May 22 '18 at 10:56
  • have you set this line mToggle.setDrawerIndicatorEnabled(true); you need to set true for drawer indicator. – pratik May 23 '18 at 11:14