Tool bar setNavigationOnClickListener breaks ActionbarDrawerToggle functionality

前端 未结 5 2114
爱一瞬间的悲伤
爱一瞬间的悲伤 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条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-15 12:39

    I think you can't use:

         t.setNavigationIcon(mHostingActivity.getV7DrawerToggleDelegate().getThemeUpIndicator());
         t.setNavigationOnClickListener(new View.OnClickListener() ...
    

    because it will break your normal navigation drawer behaviour.

    Instead try something like this in onCreateOptionsMenu(Menu menu, MenuInflater inflater):

    mHostingActivity.getDrawerToggle().setDrawerIndicatorEnabled(false);
    mHostingActivity.getDrawerToggle().setHomeAsUpIndicator(mHostingActivity.getV7DrawerToggleDelegate().getThemeUpIndicator());
    

    and then in onOptionsItemSelected

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:  
            popBackStackToTop(mHostingActivity);
            return true;
    
        default:
            break;
        }
        return false;
    }
    

    PS: don't forget to use setHasOptionsMenu(true); in your fragment onCreateView.

提交回复
热议问题