title: Dynamic options menu using invalidateOptionsMenu()
body: <p>I'm currently working on an game where I find myself in need of a dynamic options menu after implementing Google Play Game Services.  My entire game consists of strings.xml and 6 Activities (not including imported library projects).  I mention this only because it means I am building the options menu without using a layout.xml.</p>
  
  <p>What I'm trying to accomplish is changing the visible state of my signin and signout buttons based upon the users logged in status.  Currently when a user signs in the menu correctly updates itself and the Sign In button is replaced by Sign Out.  However when the Sign Out button is clicked, while it does correctly sign the user out, it does not trigger the menu update.  Attempting to Sign Out again via the button causes the application to crash because I'm not catching the exception, this I can deal with later.  The menu does update properly if you change Activities.</p>
  
  <p>What I've got so far looks remarkably similar to what I found <a href="https://stackoverflow.com/questions/13179589/change-options-menu-during-runtime-invalidateoptionsmenu">here</a></p>
  
  <p>This is my Extensions class which every other class extends to make use of methods common throughout my game, I have stripped the items unrelated to the menu.</p>
  
  <pre><code>package com.domain.myapp;
  
  import android.annotation.SuppressLint;
  import android.content.Intent;
  import android.os.Build;
  import android.view.Menu;
  import android.view.MenuItem;
  
  public abstract class Extensions extends LicenseCheck {
      private static final int SOM1= 1;
      private static final int SOM2= 2;
      private static final int SOM3= 3;
      private static final int SOM4= 4;
      private static final int SOM5= 5;
      private static final int SOM6= 6;
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          if (Build.VERSION.SDK_INT &gt;= 11) {
              buildMenu(menu);
          }
          return true;
      }
  
      @Override
      public boolean onPrepareOptionsMenu(Menu menu) {
          if (Build.VERSION.SDK_INT &lt; 11) {
              buildMenu(menu);
          }
          return true;
      }
  
      private void buildMenu(Menu menu) {
          menu.clear();
          menu.add(Menu.NONE, SOM1, Menu.NONE, "Instructions");
          menu.add(Menu.NONE, SOM2, Menu.NONE, "View Achievements");
          menu.add(Menu.NONE, SOM3, Menu.NONE, "View Leaderboard");
          menu.add(Menu.NONE, SOM4, Menu.NONE, "Sign In");
          menu.add(Menu.NONE, SOM5, Menu.NONE, "Sign Out");
          menu.add(Menu.NONE, SOM6, Menu.NONE, "Exit Game");
          if (isSignedIn()) {
              menu.findItem(SOM4).setVisible(false);
              menu.findItem(SOM5).setVisible(true);
          }
          if (!isSignedIn()) {
                  menu.findItem(SOM4).setVisible(true);
                  menu.findItem(SOM5).setVisible(false);
          }
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          switch (item.getItemId()) {
          case SOM1:
              Intent nextInstructionScreen = new Intent(getApplicationContext(), InstructionScreen.class);
              startActivity(nextInstructionScreen);   
              break;
          case SOM2:
              viewAchievements();
              break;
          case SOM3:
              viewLeaderboards();
              break;
          case SOM4:
              beginUserInitiatedSignIn();
              break;
          case SOM5:
              getGamesClient().signOut();
              menuRefresh();
              break;
          case SOM6:
              moveTaskToBack(true);
              break;
          }
          return super.onOptionsItemSelected(item);
      }
  
      @SuppressLint("NewApi")
      public void menuRefresh() {
          if (Build.VERSION.SDK_INT &gt;= 11) {
              invalidateOptionsMenu();
          }
      }
  }
  </code></pre>
  
  <p>Additionally, I spent some time trying to use onSignOutComplete() but must have been doing that wrong.  Any help would be greatly appreciated.  Using a boolean to track login status did not change the behavior at all either.  I can't figure out why the Sign Out button won't hide after it is used to sign out and make the Sign In button visible, when the opposite functionality works just fine.</p>