Android - How to hide menu option for current fragment

前端 未结 6 1484
执笔经年
执笔经年 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:16

    In the fragment where you want to hide the Item

    @Override
    public void onPrepareOptionsMenu(Menu menu) {
        MenuItem item=menu.findItem(R.id.action_search);
        item.setVisible(false);
    

    and in onCreate() of your fragment

     setHasOptionsMenu(true);
    
    0 讨论(0)
  • 2021-01-12 21:17

    Add below codes into your fragment

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }
    
    public void onPrepareOptionsMenu(Menu menu) {
        super.onPrepareOptionsMenu(menu);
        MenuItem item = menu.findItem(R.id.save);
        item.setVisible(false);
    }
    
    0 讨论(0)
  • 2021-01-12 21:23

    First get the item you want to remove :

    MenuItem item = menu.findItem(R.id.your_action);
    

    then set it's Visibility false :

    item.setVisible(false);
    

    and if the problem is in getting the menu (as it's not in the fragment), you can easily get a context from the activity that contains the menu and get the menu by it.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-12 21:36
    // create Boolean variable in the main activity
    private var menuvisibile:  Boolean = true
    
    // while navigating fragments set the menuvisibile value and use it 
    // variable as part of the return statement
    invalidateOptionsMenu()
    menuvisibile = false
    
    override fun onCreateOptionsMenu(menu: Menu?): Boolean 
        {
            val menuInflater  = menuInflater
            menuInflater.inflate(R.menu.notification,menu)
            return menuvisibile
        }
    

    working well for me.

    0 讨论(0)
  • 2021-01-12 21:39

    Adding to Muhammed's answer above. Once the item has been set as invisible, you may need to also disable the item. Note Google's comment: "Even if a menu item is not visible, it may still be invoked via its shortcut (to completely disable an item, set it to invisible and disabled)" under setVisible() in the MenuItem documentation. Thus:

    • item.setVisible(false);
    • item.setEnabled (false);
    0 讨论(0)
提交回复
热议问题