Android – onprepareoptionsmenu copies items in the actionbar

When I use onprepareoptionsmenu to add a menu item, the menu item will copy itself in the action bar. I am using the clip and creating an initial menu in the action bar of the main activity, as shown below:

...
 @Override
    public boolean onCreateOptionsMenu(Menu paramMenu) {
    super.onCreateOptionsMenu(paramMenu);
    paramMenu.add(0, 1, 0, "DashBoard").setIcon(R.drawable.ic_dashboard)
        .setShowAsAction(1);
    return true;
    }

Then I add another item to one of the clips as follows:

...
@Override
    public void onPrepareOptionsMenu(Menu paramMenu) {
    paramMenu.add(0, 2, 1, "FullScreen").setIcon(R.drawable.ic_fullscreen)
        .setShowAsAction(1);
    }

For some reason, items added through the fragment class are displayed twice... What's wrong with me?

Any help for my mistake will be greatly appreciated

resolvent:

This item may be displayed twice because you want to add it twice. See docs for onprepareoptionsmenu:

I really won't blindly add an item in onprepareoptionsmenu. You should check whether it has been added first

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>