如何使 onCreateOptionMenu 像 onResume 一样工作

how to make onCreateOptionMenu works like onResume

我的操作栏中有消息菜单,它只显示在菜单 Activity 中,每条尚未阅读的消息都会有一个警报编号,但每次我阅读消息时除非我关闭应用程序并再次打开它,否则号码不会改变,如何让它在我阅读消息后可以更改号码?我在打开消息后使用后退按钮打开菜单 activity,而不是使用 intent

打开新菜单 Activity

这是我更改操作栏警报的代码

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.actionbars, menu);
        if (mPrefs.getUserType().equalsIgnoreCase("dosen")){
            menu.findItem( R.id.chgpassword ).setVisible( false );
            menu.findItem( R.id.exit ).setVisible( false );
        } else {

            requestNotif(new ApiCallback(){
                @Override
                public void onSuccess(Integer result){
                    //do stuff here with the list from the request

                    newNotif = result;
                    MenuItem item = menu.findItem(R.id.notif);
                    LayerDrawable icon = (LayerDrawable) item.getIcon();
                    Utils2.setBadgeCount(mContext, icon, newNotif);
                }
            });

        }
        return true;
    }

您可以打电话,

invalidateOptionsMenu();

在你的 Activity 中。这会强制重新创建菜单,并在重新创建时调用方法 onPrepareOptionsMenu

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    MenuItem item = menu.findItem( R.id.notif );
   // Update the item here onwards
}

参考this and this答案。