1

I have a NavigationDrawer for my main activity, in one instance I show a fragment and the "Hamburger menu" changes into an arrow.

I disabled the ability to open the drawer via swipe when that fragment is shown by using this

mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);

and it works fine but now the problem is the user can still open the drawer by clicking the home button "Arrow". I want the arrow to act as the back button and not open the drawer so how can I stop the drawer from opening?

tyczj
  • 71,600
  • 54
  • 194
  • 296

4 Answers4

6

I guess you need to use setNavigationOnClickListener() on toolbar to override drawer behavior.

Like -

toolbar.setNavigationOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View view) {
        // logic to decide if drawer open/close, or pop fragment etc
     }
});

This does prevents drawer from opening by clicking on icon. But it does open by swiping. But that you already taken care of. So this should work.

Note This works only after you set ActionBarDrawerToggle by calling

mDrawerToggle = new ActionBarDrawerToggle(getActivity(), mDrawerLayout,  toolbar, R.string.openDrawer, R.string.closeDrawer){ ...
SachinGutte
  • 6,947
  • 5
  • 35
  • 58
  • ahhhh that was the problem, I tried that before but I never got the callback, but like you said I have to do it after I call that constructor – tyczj Apr 21 '15 at 20:52
1

Put this on your Activity:

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    if (item.getId() == android.R.id.home){
        doSomething(); //can be a finish()
    }
}

Or, from this answer

mDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        popStackIfNeeded();
        mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
        mActionBar.setDisplayHomeAsUpEnabled(false);
        mDrawerToggle.setDrawerIndicatorEnabled(true);
    }
});
Community
  • 1
  • 1
Joaquin Iurchuk
  • 5,499
  • 2
  • 48
  • 64
  • setToolbarNavigationClickListener never got called but also that code in the onClick is going to hide the home button which I do not want, the drawer indicator should act as the back button like in the Gmail app – tyczj Apr 21 '15 at 20:12
0

You could try overriding onOptionsItemSelected check for the id android.R.id.home and call finish() to go back to your previous activity.

Jayesh Elamgodil
  • 1,467
  • 11
  • 15
  • onOptionsItemSelected never gets called and I do not want to finish the activity I am in, I just want to remove the fragment – tyczj Apr 21 '15 at 19:53
  • About it not getting called, do you have statements the getActionBar().setDisplayHomeAsUpEnabled(true); getActionBar().setHomeButtonEnabled(true); in your activity. If not, could you please try adding these ? About the finish() thing, you can use your fragment manager to remove your fragment. – Jayesh Elamgodil Apr 21 '15 at 19:56
  • adding those made no difference – tyczj Apr 21 '15 at 20:08
  • Before changing the hamburger to arrow, on pressing the hamburger, does your code through onOptionsItemSelected() ? – Jayesh Elamgodil Apr 21 '15 at 20:31
  • No it does not, however reading the link from @joaquin one person suggested I am using the wrong constructor. I changed my constructor and not my onOptionsItemSelected gets called in my Activity but I still cannot stop the drawer from opening – tyczj Apr 21 '15 at 20:33
  • Are you returning super.onOptionsItemSelected(item); ? For the case of your arrow, return true. – Jayesh Elamgodil Apr 21 '15 at 20:35
  • I had to do some logic in there and yes now I am, so it is now working correctly – tyczj Apr 21 '15 at 20:45
0

try to override on options item select.

   @Override
    public boolean onOptionsItemSelected(MenuItem item) {
          switch (item.getItemId()) {
             case android.R.id.home :
                // do what you want
                return true;
          }
          return true;
       }
savepopulation
  • 11,736
  • 4
  • 55
  • 80