Android - How to hide the menu option for the current fragment

I have an ActionBar action with FrameLayout and a menu. when the user clicks a menu item, I replace the fragment with the corresponding new fragment. However, I do not 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 the menu says disabling the menu in onPrepareOptionsMenu - but how do I know which item was selected?

- Implemented solution -

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

 private Menu activityMenu; private MenuItem curMenuItem; 

Set them to onCreateOptionsMenu

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

And changed them to onOptionsItemSelected

 curMenuItem.setVisible(true); curMenuItem = activityMenu.findItem(id); curMenuItem.setVisible(false); 
+5
source share
5 answers

First get the item you want to remove:

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

then install it. Visibility false :

 item.setVisible(false); 

and if the problem is getting the menu (as it is not in the snippet), you can easily get context from the action containing this menu and get it through the menu.

+5
source

Inside the fragment you will need to use setHasOptionsMenu (true); to access the options menu from your snippet.

Code (inside your second snippet where you want to hide the element):

 @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 snippet, where you want to show that MenuItem you can do a similar thing.

+6
source

In the snippet 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 onCreate() your fragment

  setHasOptionsMenu(true); 
+4
source

Addendum to Muhammad above. Once the item is set as invisible, you may also need to disable the item. Pay attention to Google’s comment: β€œEven if the menu item is not displayed, it can still be called via its shortcut (to completely disable the item, set it invisible and disabled)” in the setVisible () file in the MenuItem documentation. Thus:

  • item.setVisible (false);
  • item.setEnabled (false);
+3
source

Add below code to your snippet

 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); } 
+1
source

Source: https://habr.com/ru/post/1207665/


All Articles