Titanium Community Questions & Answer Archive

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

Does Android not get the window events blur and focus?

As per the title, is it expected that on the android platform windows don't get blur and focus events? Is there another way to tell win a window either gains or loses focus?

— asked May 21st 2010 by Christopher Rumpf
  • android
  • blur
  • events
  • focus
  • window
0 Comments

7 Answers

  • Accepted Answer

    A nasty hack for this problem. 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
    0 Comments
  • Adding the following in your tab group setup code fixes the issue (be sure to remove it if/when the actual bug is fixed in Ti or you'll probably get the listeners called twice)

    if (Ti.Platform.osname === "android") {
        // window focus events don't work on android so this workaround
        // catches the tabGroup focus event and fires the event on the
        // active window object
        tabGroup.addEventListener('focus', function(e) {
            var win = tabGroup.activeTab.window;
            e.originalSource = e.source;
            e.source = win;
            win.fireEvent('focus', e);
        });
    }
    
    — answered July 26th 2011 by Jude Venn
    permalink
    1 Comment
    • Thank you very much, this was just what I needed :)

      — commented July 28th 2011 by Joao Silva
  • Thanks Kevin,
    It looks like you're adding an event listener to the tabGroup. I haven't tried that yet. What I have done is added listeners to the windows that are in my tabGroup. On iPhone, they seem to get tickled, on android only the open event gets caught. I'm doing something like the following:

    function winEvent(e) {
        Ti.API.info('window event> evt:' + e.type + ' window:' + e.source.name);
    }
    
    var win1 = Ti.UI.createWindow({title:'win1',name:'win1'});
    win1.addEventListener('blur', winEvent);
    win1.addEventListener('close', winEvent);
    win1.addEventListener('focus', winEvent);
    win1.addEventListener('open', winEvent);
    
    var win2 = TI.UI.createWindow({title:'win2',name:'win2'});
    win2.addEventListener('blur', winEvent);
    win2.addEventListener('close', winEvent);
    win2.addEventListener('focus', winEvent);
    win2.addEventListener('open', winEvent);
    
    var tabGroup = Ti.UI.createTabGroup();
    var t1 = Ti.UI.createTab({window:win1});
    var t2 = Ti.UI.createTab({window:win2});
    tabGroup.addTab(t1);
    tabGroup.addTab(t2);
    
    tabGroup.open();
    

    On iPhone, I'll see the event callback function reporting open, blur and focus events as you open the tabs and switch between them. on Android, I only see the open event.

    — answered May 21st 2010 by Christopher Rumpf
    permalink
    0 Comments
  • I'm baffled that this issue hasn't been fixed yet. How are we meant to detect when a tab has been accessed?

    I've got an app where the window in tab1 writes to a databse, and the window in tab2 displays the contents of that database. Its currently impossible to update the contents of the tab2 without restarting the app!

    Heres some example code which will won't work on Android:

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

    You'll find that "focus" is never fired. Its as if its simply not supported (WTF?). If you change that "focus" to be an "open" instead, you'll find "got here" is output the first time the tab/window is loaded, but it is never loaded again, regardless how many times you click into the tab.

    It seems clear theres no way of firing events each time a root window associated with a tab is opened, which severely reduces the potential uses of Titanium.

    — answered August 3rd 2010 by David Cranwell
    permalink
    3 Comments
    • David, have you had any luck with the focus event?

      — commented August 15th 2010 by Johnny Basu
    • None. From what the developer was saying above in his link to the open ticket, this is a known problem which has apparently been fixed, but not release.

      — commented August 17th 2010 by David Cranwell
    • This fixes only problem with standalone window. It isn't working if the window is attached to the TabGroup. There is already a ticket for this bug.

      — commented August 25th 2010 by Lukasz Radziwonowicz
  • I pushed a fix for Ticket 891 yesterday.

    — answered August 3rd 2010 by Don Thorp
    permalink
    3 Comments
    • has this been fixed? I am using 1.3.2 and am experiencing the same issues mentioned by David below

      — commented August 14th 2010 by Johnny Basu
    • I am using Titanium Studio, build: 1.0.4.201108101535 for Android and iOS development.

      I can hack my way around with if ( platform == blah ) then attach an eventListener, but the results are inconsistent on Android.

      I have 1 tab group, 4 windows. When I switch between window 1 and window2, window1 should be blurred (and should fire my event); window2 is focused (and should fire my event, but after looking at the debug, the 1.7.2 SDK basically says:

      Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@43ff2dc0
      

      Everything with Appcelerator has been great (sans SMS and this item), but I have to ask how hard this is to code into the SDK? I am focused on a tabGroup, yes, but I am truly focused on a Window object. When I select tab2, the visual focus is then on the Window 2 object.

      I ask "how hard this is" with sincerity because I'm having to use costly hacks to ensure an event is fired and I am a big fan of Appcelerator… "One to rule them all…"

      //Jesse

      — commented August 16th 2011 by Jesse Benedict
    • I also have the same problem, did you get a fix for this issue?

      — commented February 8th 2012 by Joseandro Luiz
  • The indexes of the focus-event on a tabGroup is always one position behind the actual.

    Example: I click on the tabs #4, #2, #3, #1.

    The events fire with those e.index: -1, 4, 2, 3

    That means, when clicking tab #1, I can't tell, because e.index is set to the previous tab (e.previousIndex is the last but one index).

    — answered June 4th 2010 by Markus Birth
    permalink
    2 Comments
    • Just noticed that this only occurs on Android. On iPhone, the events have the correct indices.

      — commented June 4th 2010 by Markus Birth
    • I am seeing this as well - looks like we have an issue already created for this.

      — commented June 4th 2010 by Kevin Whinnery
  • The focus/blur events are working for me on Android when I run through the Kitchen SInk examples. Could you share some code to demonstrate what you're seeing?

    Here's the KS example:

    tabGroup.addEventListener('focus', function(e)
    {
        messageLabel.text = 'tab changed to ' + e.index + ' old index ' + e.previousIndex;
        messageWin.open();
        setTimeout(function()
        {
            Ti.API.info('tab ' + e.tab.title  + ' prevTab = ' + (e.previousTab ? e.previousTab.title : null));
            messageLabel.text = 'active title ' + e.tab.title + ' old title ' + (e.previousTab ? e.previousTab.title : null);
        },1000);
    
        setTimeout(function()
        {
            messageWin.close({opacity:0,duration:500});
        },2000);
    
    });
    
    — answered May 21st 2010 by Kevin Whinnery
    permalink
    0 Comments
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.