24
@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 it's getting loaded, but the onPrepareOptionsMenu is not getting called at all, where I need to hide some menu-items.

Update:

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

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

Anton Kesy
  • 119
  • 7
Kevin
  • 23,174
  • 26
  • 81
  • 111
  • Thanks, I had the same problem but like you say adding a call to setHasOptionsMenu(true) in the fragments onCreate fixed it. – Jase Whatson Aug 12 '13 at 03:29

5 Answers5

23

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

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

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

trickster77777
  • 1,198
  • 2
  • 19
  • 30
15

You need to do two things. Step 1: In Fragment OnCreateView add 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.

SanRam
  • 936
  • 13
  • 13
9

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!

Carlos Borau
  • 1,433
  • 1
  • 24
  • 34
0

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);
    }
}
Ronak Jain
  • 1,723
  • 2
  • 24
  • 35
Dipti Agravat
  • 39
  • 1
  • 10
0

You must inform the system that your app bar fragment is participating in the population of the options menu. To do this, call setHasOptionsMenu(true) in your fragment's onCreate(Bundle) method

ref. https://developer.android.com/guide/fragments/appbar#activity-register

Henri
  • 1
  • 1