title: Android - How to hide menu option for current fragment
body: <p>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.</p>
  
  <pre><code>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();
        }
  </code></pre>
  
  <p>The Google documentation on <a href="http://developer.android.com/guide/topics/ui/menus.html#ChangingTheMenu" rel="noreferrer">changing menus</a> says to disable the menu in onPrepareOptionsMenu - but how will I know which item has been selected?</p>
  
  <p>--Solution Implemented--</p>
  
  <p>Using Muhammed Refaat's solution below I added two new members to the class:</p>
  
  <pre><code>private Menu activityMenu;
  private MenuItem curMenuItem;
  </code></pre>
  
  <p>Set them in onCreateOptionsMenu</p>
  
  <pre><code>activityMenu = menu;
  curMenuItem = activityMenu.findItem(R.id.action_study);
  curMenuItem.setVisible(false);
  </code></pre>
  
  <p>And changed them on onOptionsItemSelected</p>
  
  <pre><code>curMenuItem.setVisible(true);
  curMenuItem = activityMenu.findItem(id);
  curMenuItem.setVisible(false);
  </code></pre>