32

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;
}
Jeff Axelrod
  • 27,676
  • 31
  • 147
  • 246
snotyak
  • 3,709
  • 6
  • 35
  • 52
  • 2
    ActionBarSherlock does not support `TabActivity`. – CommonsWare Aug 07 '12 at 23:44
  • I used SherlockFragmentActivity with the contentView as a ViewPager+tab implementation (so I can do the swiping and click on tabs). It's not specifically a TabActivity...it's just an Activity with tabs. – snotyak Aug 07 '12 at 23:46
  • 1
    There's something wrong with the code you've posted, it won't compile. "inflater" is defined twice, and "return true" is not accepted in a method with return type void. – RenniePet Aug 19 '13 at 14:32
  • 2
    For me the information on this thread was what I needed: http://stackoverflow.com/questions/15653737/oncreateoptionsmenu-inside-fragments – RenniePet Aug 19 '13 at 14:42

2 Answers2

85

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

Jeff Axelrod
  • 27,676
  • 31
  • 147
  • 246
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

May I suggest you check out http://code.google.com/p/sherlock-demo/. As BackpackOnHead describes, it uses ViewPager + ActionBar Tabs to let you navigate two different ways between the Fragments in an Activity. In this case, it is the FragmentTabs portion of the API Demos sample app, ported to ActionBarSherlock. The LoaderThrottle Fragment in particular has its own OptionsMenu items.

Sparky
  • 8,437
  • 1
  • 29
  • 41