13

I have an activity with many fragments that uses action bar and navigation drawer. It has "home as up" enabled. I have implemented proper logic that only top level fragments show action bar drawer toggle icon, other fragments show up arrow. I achieved this by:

mDrawerToggle.setDrawerIndicatorEnabled(false);
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, mDrawerList);

Now old v4 support library ActionBarDrawerToggle became deprecated. I've switched to v7 version together with new Toolbar to get Material Design look. After that when drawer is open "up" arrow is correctly displayed, but when the above-mentioned code is executed it disappears completely.

Is it a bug in support library or I have to do something different to show "up" arrow instead of drawer indicator?

MrEngineer13
  • 38,642
  • 13
  • 74
  • 93
Andrey Novikov
  • 5,563
  • 5
  • 30
  • 51
  • the support library is as unhelpful, undocumented and problematic as it can right now, I'm having lots of problems with it. That said, try getActionBarCompat().displayHomeAsUpEnabled() or useLogo|showHome|showTitle under – MLProgrammer-CiM Oct 24 '14 at 15:59

3 Answers3

18

Answer/comments of Nikola Despotoski and Andrey Novikov are perfectly correct but I want to mention that after toolbar was replaced with following code:

drawerToggle.setDrawerIndicatorEnabled(false);
drawerToggle.setHomeAsUpIndicator(getV7DrawerToggleDelegate().getThemeUpIndicator());
setSupportActionBar(toolbar);

your activity will receive every onOptionsItemsSelected events even if you enable your drawer toogle again drawerToggle.setDrawerIndicatorEnabled(true); So you need to handle this, I've ended with

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            if (drawerToggle.isDrawerIndicatorEnabled()) {
                return drawerToggle.onOptionsItemSelected(item);
            } else {
                onBackPressed();
                return true;
            }
        default:
            return super.onOptionsItemSelected(item);
    }
}
gmsalex
  • 223
  • 1
  • 6
  • this worked till now but recently had to update support lib to v 22.1.0 and the change of hamburger icon works but when you come back to previous fragment where you want to show navigation drawer the hamburger icon is shown but it still works as up . ie does not open nav drawer. Any ideas?? – Ravi Apr 30 '15 at 11:04
  • @Ravi have you tried setting the up display based on backstack count? ( http://stackoverflow.com/a/19132453/2413303 ) – EpicPandaForce Apr 30 '15 at 11:29
  • 1
    @EpicPandaForce yes i did use this on backstack change the problem is the icon is appropriate . ie the hamburger icon but it does not open the navigation drawer anymore mDrawerToggle.setDrawerIndicatorEnabled(true); – Ravi Apr 30 '15 at 11:41
10

Have you tried to get themed up indicator using getV7DrawerToggleDelegate().getThemeUpIndicator () and set it after you disable the indicator?

Because when the indicator is disabled ActionBarDrawerToggle tries to set the previous indicator.

From ActionBarDrawerToggle source:

public void setDrawerIndicatorEnabled(boolean enable) {
    if (enable != mDrawerIndicatorEnabled) {
        if (enable) {
            setActionBarUpIndicator((Drawable) mSlider,
                    mDrawerLayout.isDrawerOpen(GravityCompat.START) ?
                            mCloseDrawerContentDescRes : mOpenDrawerContentDescRes);
        } else {
            setActionBarUpIndicator(mHomeAsUpIndicator, 0);
        }
        mDrawerIndicatorEnabled = enable;
    }
}

Edit:

As of deprecation of ActionBarActivity, you should use getDrawerToggleDelegate().getThemeUpIndicator ()

Nikola Despotoski
  • 49,966
  • 15
  • 119
  • 148
2

If you use AppCompatActivity, you can get the right drawer icon and back icon by

        if(homeUp)
    {
        getSupportActionBar().setDisplayHomeAsUpEnabled(false);
        mDrawerToggle.setDrawerIndicatorEnabled(false);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        mDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

    }
    else
    {
        mDrawerToggle.setDrawerIndicatorEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        mDrawerToggle.syncState();
    }

Without need for getV7DrawerToggleDelegate :D

kunmi
  • 692
  • 7
  • 6
  • Thank you! Only this solution works in my case for AppCompat v7 and list of fragments – Alex Feb 23 '16 at 23:43