44

I'm using the new DrawerLayout to have side navigation. I'm using the drawer icon (the 'hamburger') like this:

@Override
protected void onStart() {
    super.onStart();

    mDrawerLayout = (DrawerLayout) findViewById(R.id.activity_main_drawerlayout);
    mDrawerToggle = new ActionBarDrawerToggle(
            this, 
            mDrawerLayout, 
            R.drawable.ic_navigation_drawer, 
            R.string.app_name, 
            R.string.app_name);
    mDrawerLayout.setDrawerListener(mDrawerToggle);

    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);
}

However, when I add a Fragment to the backstack, I want to display the back arrow again, so the user can navigate back to "home", and only then open the app drawer.

How can I reset the drawer icon to the back icon?
The arrow I want:

Navigation Drawer

JJD
  • 50,076
  • 60
  • 203
  • 339
nhaarman
  • 98,571
  • 55
  • 246
  • 278

2 Answers2

73

To disable and hide the DrawerToggle "Hamburger", just call

mDrawerToggle.setDrawerIndicatorEnabled(false);
Nipper
  • 2,350
  • 1
  • 21
  • 30
  • 3
    Thanks, can you also tell how to implement back action on click after mDrawerToggle.setDrawerIndicatorEnabled(false) ? I am trying to go back to listview fragment from content detail fragment. P.S: I can go to previous fragment using back button. – adnanyousafch Jan 11 '14 at 13:22
  • 2
    @adnanyousafch http://stackoverflow.com/questions/17258020/switching-between-android-navigation-drawer-image-and-up-carat-when-using-fragme – LOG_TAG Jan 20 '14 at 03:50
  • So helpful, Thanks a alot! – Seyyed Aug 09 '16 at 06:34
  • I just want to change that hamburger icon and its back icon too. they're small and very close to the left. – Dr.jacky Nov 02 '16 at 18:40
2

I created an interface for the hosting activity to update the view state of the hamburger menu. For top level fragments I set the toggle to true and for fragments for which I want to display the up < arrow I set the toggle to false.

public class SomeFragment extends Fragment {

    public interface OnFragmentInteractionListener {
        public void showDrawerToggle(boolean showDrawerToggle);
    }

    private OnFragmentInteractionListener mListener;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            this.mListener = (OnFragmentInteractionListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        mListener.showDrawerToggle(false);
    }
}

Then in my Activity ...

public class MainActivity extends Activity implements SomeFragment.OnFragmentInteractionListener {

    private ActionBarDrawerToggle mDrawerToggle;

    public void showDrawerToggle(boolean showDrawerIndicator) {
        mDrawerToggle.setDrawerIndicatorEnabled(showDrawerIndicator);
    }

}
Bill Mote
  • 12,644
  • 7
  • 58
  • 82
  • 1
    How can I make this 'generic', so it applies to many classes like `SomeFragment`? Or does `MainActivity` have to implement each one individually? – IanB Feb 09 '16 at 22:51
  • @IanB, I think u should create someclass for example BaseFragment, implement that code, then extend every fragment that u want to use that interface with Base Fragment – WardaLyn Jan 09 '17 at 05:49