java onPrepareOptionsMenu 未在 Fragments 中调用

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

onPrepareOptionsMenu not getting called in Fragments

javaandroid

提问by Kevin

    @Override
    public void onCreateOptionsMenu(Menu menu,MenuInflater inflater){
        Log.d("Does", "get called");
        inflater.inflate(R.menu.menuItem, menu);
        super.onCreateOptionsMenu(menu,inflater);
    }

    @Override
    public void onPrepareOptionsMenu(Menu menu) {
        super.onPrepareOptionsMenu(menu);
        getActivity().invalidateOptionsMenu();
        MenuItem filter = menu.findItem(R.id.section);

        filter.setVisible(false);

    }

I am trying to load my menu in fragments and its getting loaed, but the onPrepareOptionsMenuis not getting called at all, where i need to hide some menu-items.

我正在尝试以片段的形式加载我的菜单并且它被加载,但onPrepareOptionsMenu根本没有被调用,我需要隐藏一些菜单项。

Update:

更新:

    @Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setHasOptionsMenu(true);
}

I am calling setHasOptionsMenu(true)inside my onCreate()method.

setHasOptionsMenu(true)在我的onCreate()方法内部调用。

回答by trickster77777

On Android 3.0 and higher, the options menu is considered to always be open when menu items are presented in the action bar. When an event occurs and you want to perform a menu update, you must call invalidateOptionsMenu() to request that the system call onPrepareOptionsMenu().

在 Android 3.0 及更高版本上,当菜单项出现在操作栏中时,选项菜单被视为始终处于打开状态。当事件发生并且您想要执行菜单更新时,您必须调用 invalidateOptionsMenu() 以请求系统调用 onPrepareOptionsMenu()。

http://developer.android.com/guide/topics/ui/menus.html

http://developer.android.com/guide/topics/ui/menus.html

To change particular item use: menu.findItem(R.id.your_item_id)

要更改特定项目使用: menu.findItem(R.id.your_item_id)

回答by SanRam

You need to do two things. Step 1: In Fragment OnCreateView add setHasOptionsMenu(true);

你需要做两件事。第 1 步:在 Fragment OnCreateView 添加 setHasOptionsMenu(true);

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    setHasOptionsMenu(true);
    return inflater.inflate(R.layout.fragment_user_settings, container, false);
}

Step 2: you need to add getActivity().invalidateOptionsMenu(); in your fragment in OnViewCreated. Or in mainActivity when you change the Fragment.

第二步:需要添加getActivity().invalidateOptionsMenu(); 在 OnViewCreated 的片段中。或者在更改 Fragment 时在 mainActivity 中。

回答by Carlos Borau

Probably too late, but I had same problem and the solution was really simple. Just call getActivity().invalidateOptionsMenu() from your fragment. This will call onPrepareOptionsMenu and here you can control the visibility of your items like this: menu.findItem(R.id.youritem).setVisible(true/false); Hope that helps!

可能为时已晚,但我遇到了同样的问题,解决方案非常简单。只需从您的片段中调用 getActivity().invalidateOptionsMenu() 即可。这将调用 onPrepareOptionsMenu,在这里您可以像这样控制项目的可见性: menu.findItem(R.id.youritem).setVisible(true/false); 希望有帮助!

回答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);
    }
}