Up Navigation (Action Bar's back arrow) is not working for fragments

后端 未结 2 742
隐瞒了意图╮
隐瞒了意图╮ 2021-01-05 03:15

I\'ve drawer-layout as a base layout of my activity and I\'m replacing two fragments on a frame present inside this drawer-layout. The first fragment is not added in fragmen

相关标签:
2条回答
  • 2021-01-05 03:32

    Finally got the answer. In my scenario I'm disabling the drawer indicator by mActionBarDrawerToggle.setDrawerIndicatorEnabled(false); and due to this Navigation icon clicks got disabled. To enable this I've to add ToolbarNavigationClickListener to ActionBarDrawerToggle which will enable navigation icon clicks.

    Below is the my working code :-

    mActionBarDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onBackPressed();
                }
            });
    

    Refer this link for more clarification

    0 讨论(0)
  • 2021-01-05 03:33

    After struggling with the same problem for ages, I eventually managed to get the Up button to work in fragments with this code. You must have setHasOptionsMenu set up in onCreate() or onCreateView()

    setHasOptionsMenu(true);
    

    Then, in onOptionsItemSelected() add this check for the up / home button being pressed to your switch() [or if...] statement:

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        x.L();
    
        switch (item.getItemId()) {
            case android.R.id.home :
                getActivity().onBackPressed();
    
                break;
    
            case R.id.mn_exit :
                exitFragment();
    
                break;
    
            default :
                break;
        }
    
        return super.onOptionsItemSelected(item);
    }
    
    0 讨论(0)
提交回复
热议问题