Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

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!

— asked August 2nd 2010 by David Cranwell
  • blur
  • event
  • focus
  • index
  • previoustab
  • tab
  • tabgroup
0 Comments

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){
    
        }
    )
    
    — answered August 2nd 2010 by Ricardo Alcocer
    permalink
    2 Comments
    • Yes and no. The problem is that on Android, focus() simply doesn't work on windows.

      Here is a working example, I urge everyone to try it out themselves:

      var tabGroup = Titanium.UI.createTabGroup();
      
      var win1 = Titanium.UI.createWindow({  
          title:'Favorites',
          backgroundColor:'#EEE',
         url:'favorites.js'
      });
      var tab1 = Titanium.UI.createTab({  
          icon:'KS_nav_ui.png',
          title:'Favorites',
          window:win3
      });
      win1.addEventListener('focus', function(e){
          Titanium.API.debug("got here");
      });
      
      tabGroup.open();
      

      You will find that the debugging never shows "got here".

      Am I doing something really wrong or what?

      — commented August 3rd 2010 by David Cranwell
    • Your code
      win1.addEventListener('focus', function(e){
      Titanium.API.debug("got here");
      });
      needs to be in favorites.js, not where it currently is, I believe. By the look of this you need to understand how Titanium's contexts work ….

      — commented October 13th 2010 by Jon Clarke
  • 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.

    — answered August 27th 2010 by Lukasz Radziwonowicz
    permalink
    1 Comment
    • Indeed – I have transitional advertisements where "if blur on tab1, tab2, tab3, etc then randomize advertisement." Works GREAT in iOS, makes Droid flicker because on launch, every tab except tab1 is blurred/not in focus.

      — commented August 16th 2011 by Jesse Benedict
  • 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());
    });
    
    — answered November 5th 2010 by Patrick Teglia
    permalink
    0 Comments
  • 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

    — answered August 8th 2011 by Jesse Benedict
    permalink
    1 Comment
    • BTW - this is hit and miss for Droid and I only use this for mathematical or other items.

      — commented August 16th 2011 by Jesse Benedict
  • The tabGroup is always going to be in focus unless you are hiding it.

    Why not add an event listener of focus on win2?

    — answered August 2nd 2010 by John Welch
    permalink
    0 Comments
  • You assign the focus event to the window, not the tabgroup. That's how i do.

    — answered August 2nd 2010 by Sj Singh
    permalink
    1 Comment
    • Please could you paste some code to illustrate this? I'm 100% certain i've tried this already, but I discovered on Android the "focus" event on a window is only fired once (the first time the tab is viewed) not every time the tab is viewed.

      — commented August 3rd 2010 by David Cranwell
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.