Tool bar setNavigationOnClickListener breaks ActionbarDrawerToggle functionality

前端 未结 5 2103
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-15 11:50

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 naviga

5条回答
  •  -上瘾入骨i
    2020-12-15 12:37

    Inspired by the solution of Daniel Wilson but you only have to do it once and it is all set.

    In my NavigationDrawer's setUp() (or you can do it anywhere you are initialising your ActionBarDrawerToggle instance), I write this code:

    mDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(!mDrawerToggle.isDrawerIndicatorEnabled()) {
                getActivity().onBackPressed();
            }
        }
    });
    

    Now every time android.id.home is pressed and hamburger sign is not shown, the parent activity's onBackPressed() is called.

提交回复
热议问题