You could use a OnTabChangeListener
to get a callback, when the user changed to another tab. Based on the tab ID, you can manipulate the options menu.
Call invalidateOptionsMenu()
when you need to alter the menu. Then, onCreateOptionsMenu()
will be called. You can then inflate another menu.xml
. Alternatively, you could also wait for the subsequent call to onPrepareOptionsMenu()
and toggle the visibility of single menu items via setVisible()
.
Example: (haven't tested it, but it should work more or less)
private static final String TAB_1 = "tab1";
private static final String TAB_2 = "tab2";
private TabHost mTabHost;
private final TabHost.OnTabChangeListener onTabChangedListener = new TabHost.OnTabChangeListener {
public void onTabChanged(String tabId){
invalidateOptionsMenu();
}
};
public void onCreate(Bundle b){
TabHost mTabHost = (TabHost) findViewById (R.id.tabHost);
// ...
mTabHost.setOnTabChangedListener(onTabChangedListener);
mTabHost.setCurrentTabByTag(TAB_1);
}
public boolean onCreateOptionsMenu(Menu menu){
final String currentTab = mTabHost.getCurrentTabTag();
if(TAB_1.equals(currentTab)){
getMenuInflater().inflate(R.menu.main_tab1.xml, menu);
} else if(TAB_2.equals(currentTab)){
getMenuInflater().inflate(R.menu.main_tab2.xml, menu);
}
}