How can I use onCreateOptionsMenu in a Fragment within a tab Activity? (Sherlock ActionBar)

snotyak picture snotyak · Aug 8, 2012 · Viewed 20.4k times · Source

I have the following method overridden in my tab Activity and it works fine like that but I want specific option menu's for each fragment. When I put this in my fragment and press the menu button, nothing happens.

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    MenuInflater inflater = getSupportMenuInflater();
    inflater.inflate(R.menu.activity_calc, menu);
    return true;
}

Answer

CommonsWare picture CommonsWare · Aug 8, 2012

You need to call setHasOptionsMenu(true) on your fragment before it can participate in the action bar.