How do you execute code which refers to a particular tab, each time that tab (and only that tab) is focused?
I'm tearing out my hair with this one.
app.js:
// create tab group
var tabGroup = Titanium.UI.createTabGroup();
var win1 = Titanium.UI.createWindow({
title:'Latest',
backgroundColor:'#EEE',
url:'latest.js'
});
var tab1 = Titanium.UI.createTab({
icon:'KS_nav_ui.png',
title:'Latest',
window:win1,
});
var win2 = Titanium.UI.createWindow({
title:'Tags',
backgroundColor:'#EEE',
url:'tags.js'
});
var tab2 = Titanium.UI.createTab({
icon:'KS_nav_ui.png',
title:'Tags',
window:win2
});
tabGroup.addTab(tab1);
tabGroup.addTab(tab2);
tabGroup.addEventListener('focus', function(e){
//do something specifically to the contents of the window in tab2, only when tab2 is clicked.
});
tabGroup.open();
I want to "do something" every time tab2 is clicked, but the tabgroup events model seems completely boned and the documentation is wtf. No combination of blur/open/focus, or e.index/e.tab/e.previousTab seems to ever refer to the tab which the user actually clicked!
6 Answers
-
Don't know if this is what you mean:
e.index will hold the ordinal of the tabs, zero-based.
tabGroup.addEventListener('focus',function(e){ if(e.index == 0){ } )
-
You can attach the listener to the tabGroup. It is not working properly with Android, but you can use this nasty hack. It works for me on Android platform:
tabGroup.addEventListener('focus', function(e) { setTimeout(function() { Ti.API.debug('tab changed to ' + tabGroup.activeTab); },100); });
Ticket for this bug.
-
I have a tabgroup that has 3 tabs. Each tab is it's own file.
Within the second tab's file, I have added an current window event listener and it never gets called.
var win = Titanium.UI.currentWindow; win.addEventListener('focus', function() { alert("Refreshing!"); tableview.setData(getTable()); });
-
This is aimed at the iPhone. Upon declaration of the tabs and content, I have added the following so that when one navigates away from a tab, it can update (or do whatever) in the background while the next tab presents itself.
It's a dirty hack and I haven't had it working on Droid yet, but hope this helps somewhat…
tab1.addEventListener('blur', function(){ alert('Tab1 should call something behind the scenes.'); }); tab2.addEventListener('blur', function(){ alert('Tab2 should call something behind the scenes.'); }); tab3.addEventListener('blur', function(){ alert('Tab2 should call something behind the scenes.'); }); tab4.addEventListener('blur', function(){ alert('Tab4 should call something behind the scenes.'); }); // Event Listeners declared BEFORE addition of tabGroup additions // Otherwise, pop-ups galore.... but that may be good if each tab // needs to continuously do 'something' behind the scenes... tabGroup.addTab(toc1); tabGroup.addTab(toc2); tabGroup.addTab(toc3); tabGroup.addTab(toc4); tabGroup.open();
HTH and happy coding.
-Jesse -
The tabGroup is always going to be in focus unless you are hiding it.
Why not add an event listener of focus on win2?
-
You assign the focus event to the window, not the tabgroup. That's how i do.