Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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; } 
like image 807
snotyak Avatar asked Aug 07 '12 23:08

snotyak


People also ask

How to add toolbar to fragment?

Though you can add a Toolbar anywhere within your fragment's view hierarchy, you should generally keep it at the top of the screen. To use the Toolbar in your fragment, provide an ID and obtain a reference to it in your fragment, as you would with any other view.

How do we hide the menu on the toolbar in one fragment?

How do we hide the menu on toolbar in one fragment? When you click the show button to open a fragment, you can see the fragment menu items ordered before activity menu items. This is because of the menu item's android:orderInCategory attribute value. When you click the hide button to hide the fragment.

What is onCreateOptionsMenu in android?

Find the onCreateOptionsMenu(Menu menu) method which needs to override in Activity class. This creates menu and returns Boolean value. inflate inflates a menu hierarchy from XML resource. public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().


1 Answers

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

like image 65
CommonsWare Avatar answered Oct 20 '22 23:10

CommonsWare