Android: different menus in tabgroup

You must Login before you can answer or comment on any questions.


I have a tabgroup with multiple tabs in my android app. The optionMenu should show different options wich depends on the selected tab.

I've tested this on different devices but they behave differently. On an older device running 2.3.3 the menu is the same on every tab. My 4.2.2 devices behave as expected: every tab has its particular menu.

How can I achieve this on Android 2.1 - 2.3?

Here's my current solution:

var self = Ti.UI.createTabGroup();
    self.addEventListener("open", function(e) {
        var activity = self.getActivity();
        activity.onCreateOptionsMenu = function(e) {
            menu =;
            if (self.activeTab == tab1) {
                var tab1option1 = menu.add({
                    title: "Tab 1, Option 1",
                    icon: Ti.Android.R.drawable.ic_menu_send,
                    showAsAction: Ti.Android.SHOW_AS_ACTION_ALWAYS
                var tab1option2 = menu.add({
                    title: "Tab 1, Option 2",
                    showAsAction: Ti.Android.SHOW_AS_ACTION_ALWAYS
            } else if (self.activeTab == tab2) {
                var tab2option1 = menu.add({
                    title : "Tab 2, Option 1",
                    showAsAction : Ti.Android.SHOW_AS_ACTION_ALWAYS
            } else if (self.activeTab == tab3) {
                var tab2option1 = menu.add({
                    title : "Tab 3, Option 1",
                    showAsAction : Ti.Android.SHOW_AS_ACTION_ALWAYS
    self.addEventListener("focus", function(e) {
I'm working with Titanium SDK 3.0.2 GA

Thanks in advance

2 Answers

After Jamie pushed me to this problem again, I finally found a solution.

As described in the Android Docs on 2.3 and lower the system calls onPrepareOptionsMenu() when the menu button is pressed. On Android 3.0 and higher the options menu is considered to always be open as menu items could be presented in the Actionbar.

What I've done in my Titanium application is the following thing:

I defined a function that creates my optionMenu depending on the active tab. On an API-Level lower than 11 I'm calling this function within activity.onPrepareOptionsMenu(). On higher API-Levels I only call it within activity.onCreateOptionsMenu() and I also call activity.invalidateOptionsMenu(); everytime the tabgroup gets focused. That's it! :)

Hi Manuel,

Have you tried adding the different options to separate tabs and then adding them to the tabgroup as the default tabbed application shows?

//Code taken from the default tabbed Application project
var self = Ti.UI.createTabGroup();
    //create app tabs
    var win1 = new Window(L('home')),
        win2 = new Window(L('settings'));
    var tab1 = Ti.UI.createTab({
        title: L('home'),
        icon: '/images/KS_nav_ui.png',
        window: win1
    win1.containingTab = tab1;
    var tab2 = Ti.UI.createTab({
        title: L('settings'),
        icon: '/images/KS_nav_views.png',
        window: win2
    win2.containingTab = tab2;
    return self;

— answered 3 years ago by Jamie Buckley
answer permalink
1 Comment
  • Thanks for this answer, Jamie.

    Even if your hint didn't solve my problem it somehow pointed me in the direction of the the right solution. I describe it in a seperate answer.

    — commented 3 years ago by Manuel Lehner

Your Answer

Think you can help? Login to answer this question!