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?
My application’s main Activity is a TabActivity and it contains an OptionsMenu. I define
Share
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 yourTabActivity, 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:The menu ‘belongs’ to the currently active inner tab activity, but to populate it,
onCreateOptionsMenuis automatically called on the parent activities too (bysuper).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):