3
votes

Once, my drawer icon changed from hamburger to the back button calling this:

((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);

So, the back arrow is shown, but clicking on it , the drawer is still opening.

I would like to be able to handle it, to call onBackpressed() instead.

This is when i make the instance of the drawer

   mActionBarDrawerToggle = new ActionBarDrawerToggle(getActivity(), mDrawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close) {
        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
            if (!isAdded()) return;
            getActivity().invalidateOptionsMenu();
        }

        @Override
        public View.OnClickListener getToolbarNavigationClickListener() {


            return super.getToolbarNavigationClickListener();

        }

        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);

            if (!isAdded()) return;
            if (!mUserLearnedDrawer) {
                mUserLearnedDrawer = true;
                saveSharedSetting(getActivity(), PREF_USER_LEARNED_DRAWER, "true");
            }

            getActivity().invalidateOptionsMenu();
        }
    };

I suppose that getToolbarNavigationClickListener() method must be called when I tap on the drawer toogle, no matter if its hamburger or back arrow, but it does not.

1
please show your onMenuItemClick function. It's most likely that you are overiding this function.Atif Farrukh
@maxi182 I read your comment. Sorry, I mistook your intent. So I deleted my answer.hata
Its ok, hata. thanks for try to help.maxi182

1 Answers

1
votes

When you need to override Arrow on toolbar click, set navigation click to toolbar.

getSupportActionBar().setNavigationOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            //handle back press or open drawer etc.
        }
});

It works for me every time.