title: Login/Logout with Action Bar/Menu Item
body: <p>I have a single menu item that shows up in my action bar and I would like it to display "Log In" or "Log Out" depending on whether the user is logged in or logged out. However, I cannot get it to change text because in order to do so I have to call <code>invalidateOptionsMenu()</code> from inside my <code>onOptionsSelected()</code> method. I currently have a method that updates the text that should show, and this works fine, but in order to display that text I have to recreate the options menu. </p>
  
  <p>Here is some of my code:</p>
  
  <pre><code>@Override
  public boolean onCreateOptionsMenu(Menu menu) {
      //Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.main, menu);
      this.menu = menu;
      updateMenuTitles();
      return true;
  }
  
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
      switch (item.getItemId()) {
          case R.id.login:
              if (!loggedIn) {
                  Authentication();
              } else {
                  loggedIn = false;
                  authentication = false;
                  updateMenuTitles();
                  Toast.makeText(getApplicationContext(),"Log Out Successful",Toast.LENGTH_SHORT).show();
              }
              break;
      }
      return super.onOptionsItemSelected(item);
  }
  
  private void updateMenuTitles() {
      MenuItem bedMenuItem = menu.findItem(R.id.login);
      if (loggedIn) {
          bedMenuItem.setTitle("Log Out");
      }else {
          bedMenuItem.setTitle("Login");
      }
  }
  </code></pre>