using invalidateOptionsMenu() with Login system (Android)

瘦欲@ 提交于 2019-12-22 09:14:53

问题


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;
}

回答1:


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.




回答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 :)



来源:https://stackoverflow.com/questions/11908996/using-invalidateoptionsmenu-with-login-system-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!