在登录系统(Android)中使用invalidateOptionsMenu() [英] using invalidateOptionsMenu() with Login system (Android)

查看:83
本文介绍了在登录系统(Android)中使用invalidateOptionsMenu()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让我的选项菜单重绘(从同一活动中),这称为调用对话框。

I am trying to get my options menu to redraw (from within the same activity) that I call a Login Dialog.

此处已设置。从应用程序的任何活动中,用户可以单击溢出/选项菜单,然后单击登录。会弹出一个对话框,希望他们可以成功登录。对话框然后完成。如果单击菜单,它仍然显示登录-而不是注销。看来我没有使用invalidateOptionsMenu对吗?这是代码:

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是一个对话框。当用户单击对话框中的提交按钮时,会发生这种情况:

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();

然后返回原始活动:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == 1) {

        MyActivity.this.invalidateOptionsMenu();


    }
}

使用吐司,我确认正在调用 1。

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?)

编辑:

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


推荐答案

我会仔细看此处: http://developer.android.com/guide/topics/ ui / menus.html#ChangingTheMenu

在Android 3.0+上调用 invalidateOptionsMenu()将调用 onPrepareOptionsMenu() Menu 传递给该方法,并且您想要使用该对象(无论是添加还是删除菜单项)对菜单进行更改。

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.

请记住以下内容: onPrepareOptionsMenu()


您必须返回true才能显示菜单;如果您返回false,它将不会显示。

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:您忘记了调用 super.onPrepareOptionsMenu(menu);

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

edit3:既然您确定菜单确实起作用,那么可能导致菜单不显示的唯一原因是 LoggedStatus 。确保已正确对其进行了修改,应该可以解决所有问题。

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.

这篇关于在登录系统(Android)中使用invalidateOptionsMenu()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆