How to make different menu options in different fragments?

I want to have completely different menu optionsin different fragment. I followed this post. But my fragment menu is added using the action menu. But I do not want to have an action menu in some of my fragments.
In SlidingDrawerActivity:

  @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

In my snippet:

public Friends_Status_Comment_Fragment(){
        setHasOptionsMenu(true);
    }
  @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.menu_add_comment,menu);
        super.onCreateOptionsMenu(menu, inflater);

    }

Actions elements are added using the fragment menu. How to stop him?

+4
source share
1 answer

I'm not sure that I underestimated your problem either - in your fragment you should clear the menu and create a new one - and not call super :) something like this:

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater){
    menu.clear();
    inflater.inflate(R.menu.menu_add_comment,menu);
}
+9
source

All Articles