I am trying to change my options menu (from the same action) that I call in the login dialog.
Here is the setup. From any activity in the application, the user can click on the overflow / options menu and click on the login. A dialog box will appear, and they can, hopefully, log in successfully. Then the dialog completes (). If you click on the menu, it still says "login" - not "logout". It seems I am not using invalidateOptionsMenu correctly? Here is the code:
The code of the settings menu from which the dialog is called:
case R.id.Login: Intent i = new Intent(this, Login.class); startActivityForResult(i, 0); return true;
Login.class is a dialog. When the user clicks the submit button in the dialog box, this happens:
// set log in var here Intent in = new Intent(); setResult(1, in); finish();
Then again in original action:
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == 1) { MyActivity.this.invalidateOptionsMenu(); } }
Using a toast, I confirmed that "1" is being called.
How can I cancel and redraw the menu so that it includes the logout option (since the user is now logged in?)
EDIT:
@Override public boolean onPrepareOptionsMenu(Menu menu) { MenuItem Rules = menu.findItem(R.id.Rules); MenuItem About = menu.findItem(R.id.About); MenuItem Profile = menu.findItem(R.id.Profile); MenuItem Login = menu.findItem(R.id.Login); MenuItem Logout = menu.findItem(R.id.Logout); // set the menu options depending on login status if (LoggedStatus == true) { // show the log out option Logout.setVisible(true); Login.setVisible(false); Rules.setVisible(true); Profile.setVisible(true); About.setVisible(true); } else { // show the log in option Logout.setVisible(false); Login.setVisible(true); Rules.setVisible(true); Profile.setVisible(false); // hide About.setVisible(true); } return true; }