Materialdrawer: Can't catch toolbar home button click event

Created on 10 Jul 2016  路  2Comments  路  Source: mikepenz/MaterialDrawer

I use the main activity (with MaterialDrawer) as a container for fragments. After switching to the next fragment I change home button to back arrow and lock MaterialDrawer:

getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mDrawer.getDrawerLayout().setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);

But I can't understand how to catch back arrow click. Always by clicking on the back arrow opens MaterialDrawer.

question

Most helpful comment

@mikepenz I'm not really asking this, but in any case - thanks for the help! The solution is to set the listener directly to a toolbar object. If someone will be useful, here's the code that I use to switch between the fragments:

// Enable up button only if there are entries in the back stack
        boolean isCanBack = mFragmentManager.getBackStackEntryCount() > 0;
        if(getSupportActionBar() != null) {
            if(isCanBack) {// Show back arrow and lock MaterialDrawer
                getSupportActionBar().setDisplayHomeAsUpEnabled(true);
                mDrawer.getDrawerLayout().setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
                mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) { // Home button listener
                        if(mFragmentManager.getBackStackEntryCount() > 0) {
                            mFragmentManager.popBackStack();
                        } else mDrawer.openDrawer();
                    }
                });
            } else {// Show hamburger and unlock MaterialDrawer
                getSupportActionBar().setDisplayHomeAsUpEnabled(false);
                getSupportActionBar().setDisplayShowHomeEnabled(true);
                mDrawer.getActionBarDrawerToggle().syncState();// If not call will be displayed back arrow, not hamburger 
                mDrawer.getDrawerLayout().setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
            }
        }

All 2 comments

@gmikhail if you provide the Toolbar to the DrawerBuilder it will add an ActionbarDrawerToggle for you which will open / close the drawer.

For the ActionBarDrawerToggle you might can check this:
http://iamtechnologist.blogspot.co.at/2015/03/how-to-completely-disable-android.html

@mikepenz I'm not really asking this, but in any case - thanks for the help! The solution is to set the listener directly to a toolbar object. If someone will be useful, here's the code that I use to switch between the fragments:

// Enable up button only if there are entries in the back stack
        boolean isCanBack = mFragmentManager.getBackStackEntryCount() > 0;
        if(getSupportActionBar() != null) {
            if(isCanBack) {// Show back arrow and lock MaterialDrawer
                getSupportActionBar().setDisplayHomeAsUpEnabled(true);
                mDrawer.getDrawerLayout().setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
                mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) { // Home button listener
                        if(mFragmentManager.getBackStackEntryCount() > 0) {
                            mFragmentManager.popBackStack();
                        } else mDrawer.openDrawer();
                    }
                });
            } else {// Show hamburger and unlock MaterialDrawer
                getSupportActionBar().setDisplayHomeAsUpEnabled(false);
                getSupportActionBar().setDisplayShowHomeEnabled(true);
                mDrawer.getActionBarDrawerToggle().syncState();// If not call will be displayed back arrow, not hamburger 
                mDrawer.getDrawerLayout().setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
            }
        }
Was this page helpful?
0 / 5 - 0 ratings

Related issues

wayne1203 picture wayne1203  路  3Comments

Meeks91 picture Meeks91  路  3Comments

oleynikd picture oleynikd  路  4Comments

meness picture meness  路  3Comments

sonh picture sonh  路  3Comments