I am trying to get my options menu to redraw (from within the same activity) that I call a Login Dialog.
Here is setup. From any Activity in app, user can click on overflow/options menu and click login. A dialog pops up and they can hopefully login successfully. The dialog then finish()'s. If you click on menu it still says "login" - and not "logout". It seems I am not using invalidateOptionsMenu right? Here is code:
Options Menu Code from where 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 submit button in dialog, this happens:
// set log in var's here
Intent in = new Intent();
setResult(1, in);
finish();
Then back in the Original activity:
@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 have confirmed "1" is being called.
How do I invalidate and redraw the menu so that it will then include the logout option (since 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;
}