Skip to content Skip to sidebar Skip to footer

Menu Inside Fragment Not Getting Called

@Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { Log.d('Does', 'get called'); inflater.inflate(R.menu.menuitem, menu); super.onCreateOption

Solution 1:

You'll need to make a call of setHasOptionsMenu(true); from within one of the starting lifecycle methods of the Fragment. Preferably from within onCreate(...).

In a minimalistic case the onCreate method of your Fragment looks like this:

@OverridepublicvoidonCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setHasOptionsMenu(true);
}

Also, calling super.onCreateOptionsMenu(menu, inflater); after you have inflated your custom menu will reset the menu you just have inflated to an empty menu.

So either call:

@OverridepublicvoidonCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    Log.d("Does", "get called");
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.menuitem, menu);
}

or:

@OverridepublicvoidonCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    Log.d("Does", "get called");
    //no super call
    inflater.inflate(R.menu.menuitem, menu);
}

Also, if you're testing on a Gingerbread device, the menu might not be displayed if the hosting Activity does not contain a menu item of it's own.

Post a Comment for "Menu Inside Fragment Not Getting Called"