androidandroid-fragmentsnavigation-drawerandroid-menuoncreateoptionsmenu

Android - onCreateOptionsMenu - Can't disable MenuItem from fragment


I'm trying to disable the MenuItem's in my navigationdrawer from my fragment, but it just wont work...

Fragment code:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.waiting_for_terminal, container, false);

    setHasOptionsMenu(true);
    return rootView;
}

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

        menu.findItem(R.id.nav_amount).setEnabled(false);
        menu.findItem(R.id.nav_return).setEnabled(false);
        menu.findItem(R.id.nav_about).setEnabled(false);
        menu.findItem(R.id.nav_settings).setEnabled(false);

        super.onCreateOptionsMenu(menu, inflater);
    }

I can call getTitle() for the MenuItems, and it will return correct value. But for some reason setEnabled(), setTitle(), setVisible() etc. does not work, the value stays the same...


Solution

  • Try This:
    
     @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        oldDescription= ActivityConstantUtils.sBlogDescriprtion;
        setHasOptionsMenu(true);
    }
    
    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.menu_act_add_section, menu);
        MenuItem item = menu.findItem(R.id.action_preview);
        item.setIcon(null);
        item.setTitle("");
        super.onCreateOptionsMenu(menu, inflater);
    }