Using invalidateOptionsMenu () with login system (Android)

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; } 
+4
source share
2 answers

I would carefully look at this: http://developer.android.com/guide/topics/ui/menus.html#ChangingTheMenu

Calling invalidateOptionsMenu() on Android 3.0+ will call onPrepareOptionsMenu() . A Menu is passed to this method, and you want to make changes to the menu using this object, whether adding or removing menu items.

Keep this in onPrepareOptionsMenu() :

You must return true for the displayed menu; if you return false, it will not be shown.

Edit: Sorry, I somehow missed your code at the very bottom. Let me check it out very quickly.

edit2: You forget to call super.onPrepareOptionsMenu(menu);

edit3: Now that you have determined that the menu is working, the only thing that can cause it not to display is LoggedStatus . After making sure that it is being modified correctly, everything should be decided.

+4
source

@KickingLettuce

type it into the MenuItem type. Android cannot determine if "setVisible" is applicable or can call the "Rules" variable.

MenuItem Rules = (MenuItem) menu.findItem (R.id.Rules);

which will work. hope this helps :)

-2
source

All Articles