6

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 an obvious way to remove the menu item for the selected fragment.

public class MainActivity extends ActionBarActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (savedInstanceState == null) {
            StudyFragment startFragment = new StudyFragment();
            startFragment.setArguments(getIntent().getExtras());
            getSupportFragmentManager().beginTransaction().add
                                      (R.id.container, startFragment).commit();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        switch (id) {
        case R.id.action_study:
            replaceFragment((Fragment)new StudyFragment());
            break;
        case R.id.action_list: 
            replaceFragment((Fragment)new ListFragment());
            break;
        // etc
        }
        return super.onOptionsItemSelected(item);
    }

    private void replaceFragment(Fragment f) {
        FragmentTransaction transaction =
                                getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.container, f);
        transaction.addToBackStack(null);

        transaction.commit();
      }

The Google documentation on changing menus says to disable the menu in onPrepareOptionsMenu - but how will I know which item has been selected?

--Solution Implemented--

Using Muhammed Refaat's solution below I added two new members to the class:

private Menu activityMenu;
private MenuItem curMenuItem;

Set them in onCreateOptionsMenu

activityMenu = menu;
curMenuItem = activityMenu.findItem(R.id.action_study);
curMenuItem.setVisible(false);

And changed them on onOptionsItemSelected

curMenuItem.setVisible(true);
curMenuItem = activityMenu.findItem(id);
curMenuItem.setVisible(false);
HaemEternal
  • 2,229
  • 6
  • 31
  • 50
QuantumTiger
  • 970
  • 1
  • 10
  • 22
  • https://stackoverflow.com/questions/26420204/changing-visibility-of-menu-items-in-fragment/47531110#47531110 – Ashwin H Nov 28 '17 at 12:06

6 Answers6

9

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.

Muhammed Refaat
  • 8,914
  • 14
  • 83
  • 118
  • 1
    Thanks for this - set me on the right track. I also needed to set the currently hidden item visible, but that was easy enough to work out. – QuantumTiger Nov 25 '14 at 13:51
7

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.

Atul O Holic
  • 6,692
  • 4
  • 39
  • 74
5

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);
Tushar Thakur
  • 956
  • 3
  • 15
  • 32
4

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);
DroidCrafter
  • 136
  • 1
  • 5
1

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);
}
Ardne'Jus
  • 11
  • 2
0
// 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.

Community
  • 1
  • 1
Praveenkumar
  • 31
  • 1
  • 10