How to set activeTab upon app load?

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

Am having problems with setting the active tab of my application upon load. I am trying to use tabGroup.activeTab = tabGroup.getTabs[3]; whereas the '3' is the 4th tab in my tabGroup. '0' is the first tab I believe.

app.js:

// Do it this way as to not pollute the global namespace
var sampleapplication = {}; // define app's namespace
 
Ti.include('ui.js');
 
// Use custom UI constructors to build the app's UI
sampleapplication.container = sampleapplication.ui.createApplicationContainer();
 
// open tab group
setTimeout(function() {
    sampleapplication.container.open({
        transition:Titanium.UI.iPhone.AnimationStyle.FLIP_FROM_RIGHT
    });
}, 0 //time to wait
);
ui.js:
(function(){
    sampleapplication.ui = {};
 
    sampleapplication.ui.createApplicationContainer = function() {
        var tabGroup = Titanium.UI.createTabGroup();
 
        Ti.include('includes/win1.js');
        var win1Tab = Titanium.UI.createTab({
            window:win1
        });
 
        Ti.include('includes/win2.js');
        var win2Tab = Titanium.UI.createTab({
            window:win2
        });
 
        Ti.include('includes/win3.js');
        var win3Tab = Titanium.UI.createTab({
            window:win3
        });
 
        Ti.include('includes/win4.js');
        var win4Tab = Titanium.UI.createTab({
            window:win4
        });
 
        // add tabs
        tabGroup.addTab(win1Tab);
        tabGroup.addTab(win2Tab);
        tabGroup.addTab(win3Tab);
        tabGroup.addTab(win4Tab);
 
        return tabGroup;
 
        // set active tab to the 4th (win4) tab
        tabGroup.activeTab = tabGroup.getTabs[3]; //or 'tabGroup.activeTab = tabGroup.tabs[3];'
    };
})();

3 Answers

Accepted Answer

For your code structure, you can set active tab in setTimeout() callback function.

setTimeout(
    function() {
        sampleapplication.container.setActiveTab(3);
        sampleapplication.container.open({transition : Titanium.UI.iPhone.AnimationStyle.FLIP_FROM_RIGHT});
    }, 
    0 //time to wait
);
Best,

Minh

— answered 1 year ago by Minh Nguyen
answer permalink
1 Comment
  • Awesome! The above code works, but reverse the order of the two lines. Open sampleapplication.container and then setActiveTab(3);. Thanks Minh & Matthew for all your help!

    — commented 1 year ago by Kyle Affolder

Set the activeTab property before you open() the tabGroup.

— answered 1 year ago by Matthew Hewes
answer permalink
2 Comments
  • Sorry - this was supposed to go with it: The property needs to be an integer. Just use the index #

    — commented 1 year ago by Matthew Hewes

  • How would I go about setting the activeTab property? Do I use this: var activeTab = tabGroup.activeTab(3); or maybe something like tabGroup.activeTab = tabGroup.getTabs[3]; ?

    — commented 1 year ago by Kyle Affolder

to set it before open:

tabGroup.activeTab = 3;
After opened:
tabGroup.setActiveTab(3);
Remember "tabGroup" is actually the variable you used to create the tabGroup.

— answered 1 year ago by Matthew Hewes
answer permalink
1 Comment
  • Matthew, neither one of those examples are working in my app structure. Any thoughts? Thanks for your help thus far!!

    — commented 1 year ago by Kyle Affolder

Your Answer

Think you can help? Login to answer this question!