So I've been working on and Android app that has a Navigation Bar on the top with several Tabs, and that part is working fine but now I want to be able to dynamically add Menu Items to the Action Bar from different Fragments (since some Fragments may have different options available). So far no matter what I've tried I can't seem to get the onCreateOptionsMenu to be called. Here's what I have so far
//First I have a holder class that is used to navigate between the different Fragment Tabs
public class ActionHolder extends SherlockFragmentActivity implements ActionBar.TabListener {....
//And then I have this method for switching Fragments based on what Tab is selected
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
int selectedTab = tab.getPosition();
if (selectedTab == 0) {
SalesMainScreen salesScreen = new SalesMainScreen();
ft.replace(R.id.content, salesScreen);
}
else if (selectedTab == 1) {
ClientMainScreen clientScreen = new ClientMainScreen();
ft.replace(R.id.content, clientScreen);
}.....
Now here is one of the Tab's Fragments (the SalesMainScreen) that I want to have a few menu items added to the Action Bar
@Override
public void onCreate (Bundle savedInstanceState) {
Log.i("message","the oncreate method was called");
setHasOptionsMenu(true);
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup group, Bundle saved) {
return inflater.inflate(R.layout.salesmainscreen, group, false);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Log.i("message", "the oncreatemenu method called");
inflater.inflate(R.menu.menu_refresh, menu);
super.onCreateOptionsMenu(menu, inflater);
}
I see the OnCreate Log message being called but I don't see the onCreateOptionsMenu Log being called at all. Also, I know that sometimes the imports cause issues, but when I import the Sherlock Menu and Menu Inflater I get all kinds of error messages on the OnCreateOptionMenu method about them not being compatible. Is it possible in this setup to dynamically add Menu Items to the Action Bar, or should I just add the items and then just don't do any actions on the ones that don't apply to the fragment that is being displayed?
I have an app using
SherlockActionBar
and tabs, with each tab containing aSherlockFragment
. The main activity has its own menu in the action bar, and one of the fragments adds a search item to the action bar menu.The main activity has the following:
The fragment in the tab has the following:
When I start the main activity, the tab shows
ThingOneFragment
by default and I see the search icon in the action bar. When I select the other tabs, the search icon disappears. You do need to make sure that you are using the Sherlock classes forMenu
,MenuInflater
, etc.I'm not sure if it makes a difference but my
TabListener
looks like this:I'm not sure if that is causing your issue or if it's even the correct way of handling tabs but I include it for completeness.