Titanium Community Questions & Answer Archive

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

Tab refresh - window to previous state

This is probably a big newbie question, but I'm stuck…

I have a tab (tab 1) that shows window A. Inside window A something happens and then window A is changed into window B.

Now what I need is when I go to tab 2, and then back to tab 1 that window A is shown again. And not window B.

I believe it has to do with the focus, but I can't find out how to achieve this.

Could someone help me please?

— asked November 4th 2010 by Jeroen Onstenk
  • focus
  • iphone
  • tab
0 Comments

8 Answers

  • Simple example where you enter text in the first tab and when you come back to the tab, the value is reset.

    You can test that it only resets after you have gone to another tab by modifying the text in the first tab, then closing and reopening the application. When you reopen, the original text should remain since you have not closed the application… you only put it in the background.

    // this sets the background color of the master UIView (when there are no windows/tab groups on it)
    Titanium.UI.setBackgroundColor('#000');
    
    // create tab group
    var tabGroup = Titanium.UI.createTabGroup();
    
    
    //
    // create base UI tab and root window
    //
    var win1 = Titanium.UI.createWindow({  
        title:'Tab 1',
        backgroundColor:'#fff'
    });
    var tab1 = Titanium.UI.createTab({  
        icon:'KS_nav_views.png',
        title:'Tab 1',
        window:win1
    });
    
    var textField1 = Titanium.UI.createTextField({
        color:'#999',
        font:{fontSize:20,fontFamily:'Helvetica Neue'},
        textAlign:'center',
        top:10,
        left:10,
        height:35,
        width:250,
        borderStyle:Titanium.UI.INPUT_BORDERSTYLE_ROUNDED
    });
    
    win1.add(textField1);
    
    //
    // create controls tab and root window
    //
    var win2 = Titanium.UI.createWindow({  
        title:'Tab 2',
        backgroundColor:'#fff'
    });
    var tab2 = Titanium.UI.createTab({  
        icon:'KS_nav_ui.png',
        title:'Tab 2',
        window:win2
    });
    
    var label2 = Titanium.UI.createLabel({
        color:'#999',
        text:'I am Window 2',
        font:{fontSize:20,fontFamily:'Helvetica Neue'},
        textAlign:'center',
        width:'auto'
    });
    
    win2.add(label2);
    
    //
    //  add tabs
    //
    tabGroup.addTab(tab1);  
    tabGroup.addTab(tab2);  
    
    // open tab group
    tabGroup.open();
    
    //
    // when the tabGroup gets focus, check to see if the first
    // tab is the one getting the focus, if so reset it's value if
    // it was not the last tab to have the focus
    tabGroup.addEventListener('focus', function(e){
        if (e.index == 0 && e.previousIndex != 0) {
            // reset to original state
            textField1.value = '';
        }
    });
    
    — answered December 8th 2010 by Aaron Saunders
    permalink
    1 Comment
    • That's a good solution if you're just talking about changing window content but I think the question is similar to my own. In my app, each tab is a table view similar to Kitchen Sink, some with quite a few levels of drill down. What I want is the tab to reset to the first table view window when the tab is changed.

      Perhaps adding a blur listener to each drilled down window and checking if the active tab is the parent or not, then closing each window accordingly? How can I check the parent like this?

      — commented August 24th 2011 by dave fletcher
  • still no answer for this ? i got the same problem

    — answered December 8th 2010 by Vitalij Dadaschjanz
    permalink
    2 Comments
    • Nope, unfortunately not. I took the function out. Maybe with this bump the issue gets new attention?

      — commented December 8th 2010 by Jeroen Onstenk
    • updated question with answer

      — commented December 8th 2010 by Aaron Saunders
  • just checking back to see if you ever got this working

    — answered December 10th 2010 by Aaron Saunders
    permalink
    1 Comment
    • I'm sorry I didn’t reply earlier. I took the function completely out. But I will try it out soon, because I do am curious about it.

      — commented January 9th 2011 by Jeroen Onstenk
  • I think the fact that switching Tabs does not close previous subwindows is a missing feature. All Native Apps on the iphone i checked, do this.

    — answered January 6th 2011 by David P.
    permalink
    0 Comments
  • you should be use "window focus event".

    Titanium.UI.currentWindow.addEventListener('focus',winopened);
    function winopened(e)
    {
       alert("win opened");
    }
    
    — answered July 7th 2011 by yalcin savas
    permalink
    0 Comments
  • I have the same question, but specifically:

    I have a "home" tab and other tabs. "home" is the default (first) tab opened. The user can then click through several windows from this one, all opened with Titanium.UI.currentTab.open.

    If the user clicks another tab, and then clicks the "home" tab again, I need the initial "home" window to display.

    How do I achieve this so that it works on both iPhone and android?

    — answered August 16th 2011 by Robert Turrall
    permalink
    3 Comments
    • hey Robert,

      I need to achieve the same functionality. Did you have any success with it ? In my case even focus event listener is not working. How can i refresh the tab on use focus ? By the way i am working on android.

      Regards,

      wahib

      — commented September 9th 2011 by wahib haq
    • Hi Wahib

      No, nothing yet. Seems to have joined the pile of questions that never get answered…

      — commented September 19th 2011 by Robert Turrall
    • you can check this solution: http://developer.appcelerator.com/question/119336/how-to-return-to-the-root-window-of-my-ta

      — commented December 14th 2011 by Daniel Urstöger
  • Try doing this. Just open the window you want it to open along with the change you want to make in that window on the tabView's (focus) event listener.

    tabGroup.addEventListener('focus', function(e){
        if (e.index == 0 && e.previousIndex != 0) {
            // reset to original state with the first window on your tab
            win1.open();
            textField1.value = '';
        }
    });
    
    — answered November 21st 2012 by Sudeep Agarwal
    permalink
    0 Comments
  • Hi Jeroen,

    You MIGHT theoretically be able to do that if you put "Ti.UI.currentWindow.close();" in a blur event listener in Window B.

    Titanium.UI.currentWindow.addEventListener('blur', function(e){
        Ti.UI.currentWindow.close();
    });
    

    However that would only work if you didn't open any windows FROM window B!

    Or maybe do the same in the tabgroup click listener:

    Titanium.UI.currentTabGroup.addEventListener('click', function(e){
        Ti.UI.currentWindow.close();
    });
    

    …just a couple of options to play with.

    cheers,
    Chris.

    — answered November 4th 2010 by Chris Reed
    permalink
    1 Comment
    • Thanks for your reply. Unfortunately it doesn’t seem to work well. I played with it all over the file (-: But no success.

      — commented November 4th 2010 by Jeroen Onstenk
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.