kanytu / android-material-drawer-template

An Android template with navigation drawer for material design
Apache License 2.0
674 stars 217 forks source link

How to add callback on home button is pressed? #49

Open yhojann-cl opened 9 years ago

yhojann-cl commented 9 years ago

Try change home button with mNavigationDrawerFragment.showBackButton() for back to previous fragment but how to trigger on button is pressed and prevent open drawer? onOptionsItemSelected(MenuItem item) does not work.

yhojann-cl commented 9 years ago

By using "showBackButton ()" to change the navigation button on the menu and still calling does not work as it should and the back button.

Solution for me, to navigate between fragments:

public void showBackButton() {
    if (getActivity() instanceof ActionBarActivity) {
        ((ActionBarActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    //Disables onClick toggle listener (onClick)
    mActionBarDrawerToggle.setDrawerIndicatorEnabled(false);
    mActionBarDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                if (mOnBackPressed != null)
                    mOnBackPressed.call();
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    });

    mToolbar.setNavigationIcon(((ActionBarActivity)getActivity()).getV7DrawerToggleDelegate().getThemeUpIndicator());
    mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
}

mOnBackPressed is Callable:

public void setup(int fragmentId, DrawerLayout drawerLayout, Toolbar toolbar){
     this.setup(fragmentId, drawerLayout, toolbar, null);
}

public void setup(int fragmentId, DrawerLayout drawerLayout, Toolbar toolbar, @Nullable Callable onBackPressed) {
        mToolbar = toolbar;
        mOnBackPressed = onBackPressed;
        ...