My application's main Activity is a TabActivity and it contains an OptionsMenu. I define some other Activities (which go into a tab) and I would like to define a menu in each of these activities and have its menu merged with the main one. Is it possible?
-
I don't think its possible because Android focus on one activity in Tab Activities. You if you are going to merge the menu for each activity with the Main menu. Its don't make sense. But what you can do it you can inflate the other activities menu in Main Tab Activity and add other menu option to you main activity menu. – Arslan Anwar Nov 22 '11 at 19:40
2 Answers
Yes, this is possible. Basically you just inflate multiple xml files into the same options menu. Items are added to the menu in order of inflation.
Just overwrite onCreateOptionsMenu(Menu menu)
for your TabActivity
, inflating the xml file containing the main options. Then overwrite it for each of your inner tab activities, inflating the tab-specific options. Just write them as you usually would:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.options, menu);
return super.onCreateOptionsMenu(menu);
}
The menu 'belongs' to the currently active inner tab activity, but to populate it, onCreateOptionsMenu
is automatically called on the parent activities too (by super
).
However, strangely, onMenuItemSelected(int featureId, MenuItem item)
does not do the same. To handle item selection, the inner tab activities still have to explicitly call the corresponding method on the parent activity (after you determine that the selected option is not tab-specific):
return super.onMenuItemSelected(featureId, item)
|| getParent().onMenuItemSelected(featureId, item);

- 4,225
- 4
- 31
- 55
-
I think I'll declare the complete menu in each TabActivity... :) – Carles Company Nov 25 '11 at 11:07
-
I don't know, I feel it's a little too messy. (It isn't but I have a weird feeling) – Carles Company Nov 25 '11 at 11:29
-
I personally do not like to repeat code, so I use the above method. Objectively speaking it's simpler to maintain. And the fact that `super.onCreateOptionsMenu` automatically calls parent activities makes me believe that this is what the Android developers intended. But of course you should use the code you're more comfortable with. – mhelvens Nov 25 '11 at 11:37
-
You're right. I suppose I was hoping for a simple flag that would allow this :). I'm still getting used to the Android (an Java's) way of doing things... – Carles Company Nov 25 '11 at 15:20
-
It does not works for android 3.0 and above. Does any one tried for Action Bar ? – Sachchidanand Jul 31 '12 at 15:01
Are you creating the menus dynamically or in separate XMLs?
if Dynamically you can just set it up as
public void createMenu()
{
//Main Menu here
switch(tab)
{
case '1': //add tab 1 menu
break;
case '2': //add tab 2 menu
break;
}
}

- 695
- 7
- 27
-
I create them in separate XMLs. I have no problem to create them programatically if there's no other option but I'd rather keep it in XML. – Carles Company Nov 21 '11 at 12:18