android-material-drawer-template icon indicating copy to clipboard operation
android-material-drawer-template copied to clipboard

How to add callback on home button is pressed?

Open yhojann-cl opened this issue 10 years ago • 1 comments

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 avatar Oct 04 '15 00:10 yhojann-cl

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;
        ...

yhojann-cl avatar Oct 06 '15 16:10 yhojann-cl