在prepareOptionsMenu没有得到所谓的碎片 [英] onPrepareOptionsMenu not getting called in Fragments

查看:198
本文介绍了在prepareOptionsMenu没有得到所谓的碎片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    @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);

    }

我试图加载我的菜单中的片段和其越来越loaed,但在prepareOptionsMenu 是没有得到调用,在这里我需要隐藏一些菜单项。

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

更新:

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

我打电话 setHasOptionsMenu(真)在我的的onCreate()方法。

推荐答案

在Android 3.0及更高版本,选项菜单被认为始终是打开的,当菜单项psented操作栏中$ P $。当事件发生时,你要执行菜单更新,您必须调用invalidateOptionsMenu()来请求对prepareOptionsMenu()。

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().

<一个href="http://developer.android.com/guide/topics/ui/menus.html">http://developer.android.com/guide/topics/ui/menus.html

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

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

这篇关于在prepareOptionsMenu没有得到所谓的碎片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆