java Android - 刷新选项菜单而不调用 invalidateOptionsMenu()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14867458/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 17:40:56  来源:igfitidea点击:

Android - Refresh Options Menu without calling invalidateOptionsMenu()

javaandroidoptions-menu

提问by Michael 'Maik' Ardan

Hello Android Developers,

您好 Android 开发者,

I have seen a lot of question regarding the update of Options Menu. However, every answer says I have to call invalidateOptionsMenu().

My question is, is there any other way of updating the Options Menu without invoking the method invalidateOptionsMenu()?

我看到很多关于选项菜单更新的问题。但是,每个答案都说我必须打电话invalidateOptionsMenu()

我的问题是,有没有其他方法可以在不调用该方法的情况下更新选项菜单invalidateOptionsMenu()

回答by Michael 'Maik' Ardan

@Override
    public boolean onPrepareOptionsMenu(Menu menu) {

        menu.clear(); // Clear the menu first

            /* Add the menu items */

        return super.onPrepareOptionsMenu(menu);
    }

This solved the problem on updating the OptionsMenu without invoking the invalidateOptionsMenu()

这解决了更新 OptionsMenu 而不调用 invalidateOptionsMenu()