4

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;
}
TheLettuceMaster
  • 15,594
  • 48
  • 153
  • 259
  • What OS are you testing this on? And, are you using the support package, ActionBarSherlock...? – Cat Aug 10 '12 at 20:58
  • What are you expecting to happen? You have a menu item called login. Unless you change it, hide it, disable it, or remove it then it's going to show up. – Falmarri Aug 10 '12 at 21:27
  • I have the method onPrepareOptionsMenu() where I do this. I just didn't want t show ALL the code. I am wondering if I am using the invalidate code out of range? – TheLettuceMaster Aug 10 '12 at 21:40
  • can you post your `onPrepareOptionsMenu()` code? I don't see anything obviously wrong with the code you've posted. Maybe place the result check in `onActivityResult(..)` before you call `super.onActivityResult(..)`? – Alex Curran Aug 10 '12 at 22:41
  • @Espiandev hmm... I switched the two as you said and no difference. I added the onPrep method in question. – TheLettuceMaster Aug 10 '12 at 23:17

2 Answers2

4

I would take a close 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 that method and you want to make the changes to menu using that object, whether it be adding or removing menu items.

Keep this in mind for onPrepareOptionsMenu():

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

edit: sorry I somehow missed your code at the very bottom. Let me check this over real quick.

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

edit3: Now that you have determined the menu does work, the only thing that could be causing it to not display is LoggedStatus. Making sure that it is properly being modified should solve everything.

telkins
  • 10,440
  • 8
  • 52
  • 79
  • Thank you, I will take a look at that. As you see up top, I am returning true. I'm also aware of the concept of invalidateOptionsMenu(), and when the code hits it (as seen above), it does NOT trigger onPrepareOptionsMenu in my case. – TheLettuceMaster Aug 13 '12 at 21:34
  • @KickingLettuce I found your problem! You are forgetting to call the super method in onPrepareOptionsMenu(). Also, if you noticed my previous comment before about setting the visibility, you can ignore that because setting the visibility is fine. – telkins Aug 13 '12 at 21:37
  • I will Check into that. I guess my whole question is, why isn't invalidateOptionsMenu() refreshing my menu? I just had a thought: How my menu is displayed is based on the "LoggedStatus" variable. It seems the problem is not where i'm invalidating, but maybe that LoggedStatus is not being "refreshed" and updated after the user logs in. That is done in a different class. When the dialog dismisses, and the above class is back in focus, I need to confirm if LoggedStatus is being changed from false to true. Does this sound like a potential issue? – TheLettuceMaster Aug 13 '12 at 21:46
  • 1
    @KickingLettuce Have you tried my suggestion of adding the call to `super`? Without it the method won't use any Android internal code and essentially do nothing, and that's the only thing that looks wrong with your code. Once you've determined that it is updating I would worry about the LoggedStatus stuff. – telkins Aug 13 '12 at 21:55
  • I didn't notice that edit sorry. I believe since I asked this original question I have included that, though in my most recent code I have "return super.onPrepareOptionsMenu(menu);" Upon further thought, not sure why I did that. Using your code, does that go before everything else or last? – TheLettuceMaster Aug 13 '12 at 22:09
  • 1
    @KickingLettuce I would try it like how this SO answer does it: http://stackoverflow.com/questions/4199753/how-can-i-alter-a-menuitem-on-the-options-menu-on-android – telkins Aug 13 '12 at 22:28
  • Ok, I will try these suggestions when I can test app and hopefully mark correct soon! – TheLettuceMaster Aug 13 '12 at 22:33
  • Well, I did figure it out, apparently invalidateOptionsMenu() was working ok. But the LoggedStatus variable was not being changed. I simply had to make a new call/reference tot he variable before I invalidated and it works great. If you want to taylor your answer to this solution, I will mark you correct for the +100, otherwise, I'll just answer it. Thanks again! – TheLettuceMaster Aug 14 '12 at 16:20
  • @KickingLettuce Good to hear! I made a last edit to my post for you. I always try to remember to test any problem I have in chunks such that it is easy to isolate the logic error with LoggedStatus from the display issue with onPrepareOptionsMenu, I've found it to be much easier. Anyways, congrats. :) – telkins Aug 14 '12 at 16:57
-2

@KickingLettuce

cast it to MenuItem type. Android cannot determine if "setVisible" is applicable or can invoke for "Rules" variable.

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

that will work. hope it helps :)