Android - How to hide menu option for current fragment

前端 未结 6 1486
执笔经年
执笔经年 2021-01-12 20:39

I have an ActionBar activity with a FrameLayout and a menu. when the user clicks the menu item I replace the fragment with the relevant new fragment. However, I cannot see a

6条回答
  •  说谎
    说谎 (楼主)
    2021-01-12 21:26

    Inside your fragment you will have to use setHasOptionsMenu(true); in order to access options menu from within your fragment.

    Code (inside your second fragment where you wanna hide the item):

    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setHasOptionsMenu(true);
    }
    
    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
      // TODO your code to hide item here
      super.onCreateOptionsMenu(menu, inflater);     
    }
    

    Similarly, for your fragment where you want to show that MenuItem you can do the similar thing.

提交回复
热议问题