Daniel Wilson Daniel Wilson - 3 months ago 92
Android Question

Tool bar setNavigationOnClickListener breaks ActionbarDrawerToggle functionality

I'm swapping out the action bar for the tool bar, and I nearly have every piece of the puzzle in place. My issue is specifically if I navigate 'up' and restore the navigation drawer, the drawer toggle button no longer works. I figured out if I set the drawer mode to unlocked I have the ability to swipe to open the drawer, but can't click to open the drawer.

So I load fragment A, drawer behaviour is fine, go down to fragment B and apply the up icon, hit up to go back to A, and the drawer won't open with a click any more.

Entering Fragment B:

Toolbar t = mHostingActivity.getToolbar();
mHostingActivity.getDrawerToggle().setDrawerIndicatorEnabled(false);
mHostingActivity.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
t.setNavigationIcon(mHostingActivity.getV7DrawerToggleDelegate().getThemeUpIndicator());
t.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popBackStackToTop(mHostingActivity);
}
});

/**
* Pop the back stack and hide the Up caret all the way to the top level of the {@link com.loylap.activities.MainActivity}
*
* @param activity our hosting activity
*/
public static void popBackStackToTop(MainActivity activity) {
if (activity != null) {
FragmentManager fm = activity.getSupportFragmentManager();
fm.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
activity.getDrawerLayout().setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
activity.getDrawerToggle().setDrawerIndicatorEnabled(true);
}
}


The navigation drawer is set up just like the sample, maybe the old way of setting up the options is the issue? For example, I still have this in my activity:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}

return super.onOptionsItemSelected(item);
}


EDIT:

Okay so I've narrowed down the issue to the setNavigationOnClickListener(). If I don't set this (and go up via back button) - the drawer behaves correctly. So now the question is how do I correctly allow the user to go 'up', and restore the click listener after to we do go up?

Answer

So I've figured out I was creating the wrong click listener. Instead of setNavigationOnClickListener(), I need setToolbarNavigationClickListener() :)

A subtle but important change, now the tool bar is behaving in partnership with the v7 ActionBarDrawerToggle

/**
 * Create the Up caret for a lower level fragment {@link com.loylap.activities.MainActivity}
 *
 * @param activity our hosting activity
 */
public static void createUpButton(final MainActivity activity)
{
    ActionBarDrawerToggle toggle = activity.getDrawerToggle();
    //Disables onClick toggle listener (onClick)
    toggle.setDrawerIndicatorEnabled(false);
    toggle.setToolbarNavigationClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            popBackStackToTop(activity);
        }
    });

    Toolbar t = activity.getToolbar();
    t.setNavigationIcon(activity.getV7DrawerToggleDelegate().getThemeUpIndicator());
    activity.getDrawerLayout().setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
}