Android 在 Fragment 中使用 onPrepareOptionsMenu 而不是 onCreateOptionsMenu

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11375981/
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-08-20 07:12:08  来源:igfitidea点击:

Using onPrepareOptionsMenu instead of onCreateOptionsMenu in Fragment

androidandroid-fragmentseventsmenuandroid-menu

提问by Gerardo Contijoch

I had a problem setting up some fragment menu items in the ActionBarand I found a way to solve it, but I don't understand why it worked.

我在 中设置一些片段菜单项时遇到问题ActionBar,我找到了解决它的方法,但我不明白它为什么起作用。

I wanted to change the visibility in a menu item right after I inflated it from a menu xml file in onCreateOptionsMenumethod. The code seems to work fine, but there's no visible effect. I solved the problem inflating the menu in onCreateOptionsMenumethod but changing the visibility of it in onPrepareOptionsMenumethod.

我想在我从方法中的菜单 xml 文件膨胀后立即更改菜单项中的可见性onCreateOptionsMenu。代码似乎工作正常,但没有明显的效果。我解决了在onCreateOptionsMenu方法中膨胀菜单但在方法中改变它的可见性的问题onPrepareOptionsMenu

What I want to know is why changing the visibility in onCreateOptionsMenudoes not work.

我想知道的是为什么改变可见性onCreateOptionsMenu不起作用。

What can I do in onPrepareOptionsMenuthat I can't do in onCreateOptionsMenu?

我能做什么onPrepareOptionsMenu,我不能做onCreateOptionsMenu什么?

Is there any pattern to follow here?

这里有什么模式可以遵循吗?

Thanks!

谢谢!

Here's the relevant code, just in case:

这是相关的代码,以防万一:

public class MyFragment extends Fragment {

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.my_menu, menu);

        // This does not work, compiles and runs fine, but has no visible effect
        MenuItem someMenuItem = menu.findItem(R.id.some_menu_item);
        someMenuItem.setVisible(false);
    }

    @Override
    public void onPrepareOptionsMenu(Menu menu) {
        super.onPrepareOptionsMenu(menu);

        // This does work
        MenuItem someMenuItem = menu.findItem(R.id.some_menu_item);
        someMenuItem.setVisible(false);
    }
}

采纳答案by Christian

You should call super.onCreateOptionsMenu(menu, inflater);after you have created your menu, not before. That sends the menu up in the hierarchy and other fragments or the activity may want to add items themselves.

您应该super.onCreateOptionsMenu(menu, inflater);在创建菜单后调用,而不是之前调用。这会在层次结构和其他片段中向上发送菜单,或者活动可能想要自己添加项目。

The activity is responsible for the displaying and managing the menu, so if you change the visibility after it has been sent to the activity, nothing much is going to happen.

活动负责显示和管理菜单,因此如果您在将其发送到活动后更改可见性,则不会发生任何事情。

Now, when you call super.onPrepareOptionsMenu(menu);it will "prepare" it's menu, but it will now take the changes you've made in the onCreateOptionsMenuinto account.

现在,当你调用super.onPrepareOptionsMenu(menu);它时,它会“准备”它的菜单,但它现在会考虑你所做的更改onCreateOptionsMenu

回答by r3d

May be the code should return true to make the menu visible, that means you should put return true;statement in both onCreateOptionsMenu()and onPrepareOptionsMenu().

可能是代码应该返回 true 以使菜单可见,这意味着您应该return true;onCreateOptionsMenu()和中都放置 语句onPrepareOptionsMenu()

Hope this helps.

希望这可以帮助。

回答by Howard

I use the onPrepareOptionsMenu to update which items on the menu should be active and which ones should be grayed out/removed depending on the current state of the activity. Use the setVisible method of the menu item to control whether it is currently shown on the menu or not.

我使用 onPrepareOptionsMenu 更新菜单上的哪些项目应该处于活动状态,哪些项目应该根据活动的当前状态变灰/删除。使用菜单项的 setVisible 方法来控制它当前是否显示在菜单上。

回答by Dipti Agravat

This works for me

这对我有用

public class ContentFragment extends android.support.v4.app.Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.content_frame,container,false);
    setHasOptionsMenu(true);
    return v;
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.note_menu,menu);
    super.onCreateOptionsMenu(menu, inflater);
}
}

Try it

尝试一下