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 to show the root window after opening multiple windows

Hi there,

When I create a single tab inside a tab group and create some windows in it, the windows are stacked and I get a 'back' navigation button when running it on the iPhone. How do I get back to the root window, without clicking away multiple stacked windows?

I guess I need to call window.close() on each child window in turn.

Example:
app>win1>win2>win3>win4

I do close win4, then win3, then win2 and then win1. Unfortunately after I close win4 to win1 I still can see the nav bar from win3 with not responding 'back' button instead of the app's nav bar.

— asked April 20th 2010 by Adam Niklaus
  • iphone
  • navbar
  • tab
  • windows
0 Comments

5 Answers

  • Accepted Answer

    I manage this by doing the following, everytime you open a new window, pass a pointer to the 'old' window with it like this:

    Titanium.UI.createWindow({
        url:'somenewcode.js',
        title:'your new window',
        oldWin:Ti.currentWindow // this gets you a pointer
    });
    

    In the 'new' window you can 'close' this by calling : 'Ti.currentWindow.oldWin.close()'
    With this knowledge, you can make a 'go to root window' function by checking for oldWin's in the windows.
    I've made it like this:

    if(Ti.currentWindow.oldWin.oldWin.oldWin) { // 4 windows deep
        Ti.currentWindow.oldWin.oldWin.oldWin.close();
    }
    if(Ti.currentWindow.oldWin.oldWin) { // 3 windows deep
        Ti.currentWindow.oldWin.oldWin.close();
    }
    if(Ti.currentWindow.oldWin) { // 2 windows deep
        Ti.currentWindow.oldWin.close();
    }
    Ti.currentWindow.close(); // 1 window deep
    

    It looks dirty (and it probably is), but it gets the job done.

    — answered September 12th 2010 by Jeroen Kntl
    permalink
    5 Comments
    • Ha, you come across helping me solving another issue.. thanks.

      — commented September 13th 2010 by Andy Wang
    • great! you make my day. thank!

      — commented October 5th 2010 by Soeren Damrau
    • Seems like this is the only option around.

      Just as a clarification, you need to close the most deep window after the root.
      No need to close every window on the stack.

      — commented May 23rd 2011 by Roberto Colnaghi
    • Under the current release this option only closes the current window.

      — commented February 28th 2012 by Francis Meetze
    • This works. But we need to use Ti.UI option
      Between, I have another query. How can I check whether the Ti.UI.currentWindow.oldWin exists.Because when I check this in a condition, it shows error. Is there any way to check oldWin exists in current window.

      if(Ti.UI.currentWindow.oldWin) { // 2 windows deep
      Ti.UI.currentWindow.oldWin.close();
      }

      — commented August 12th 2012 by Rebin Joseph
  • Apple's docs include the following tidbit:

    User taps always display the root view of the tab, regardless of which tab was previously selected. This is true even if the tab was already selected.

    So you should be able to achieve your goal by emulating a click on the tab icon, eg:

    tabGroup.setActiveTab(0);

    or:

    Ti.UI.currentTab.fireEvent('click');

    Not tested.

    — answered September 13th 2010 by James K
    permalink
    1 Comment
    • As this is the default and expected behavior on iOS, it should work. It doesn't.
      That should be considered a Titanium bug.

      — commented May 23rd 2011 by Roberto Colnaghi
  • That post is old, but I've found a little beautiful trick.

    Write this code in all windows, modals, etc

    window.addEvent('backToFirstWindow', function ( ) {
        Ti.API.tabGroup.getActiveTab().close();
    }
    

    When you want go back to first window, you dispatch a event like this:

    Ti.API.fireEvent("backToFirstWindow");
    

    The event will bubble until your first window.

    — answered March 8th 2013 by Raphael Luchini
    permalink
    4 Comments
    • Nice one, thank you!

      — commented June 28th 2013 by Predrag Ristic
    • Not working for me

      — commented December 26th 2014 by Igor Kravchenko
    • The above code is missing a bracket.

      window.addEvent('backToFirstWindow', function ( ) {
          Ti.API.tabGroup.getActiveTab().close();
      )}
      

      — commented February 20th 2015 by Park Via
    • Actually should be this,

      window.addEvent('backToFirstWindow', function () {
          Ti.API.tabGroup.getActiveTab().close();
      });
      

      — commented February 20th 2015 by Park Via
  • typing oldwin a bunch of times seems silly:

    function closeStack(win) {
        if (win == null) {
            return;
        }
        closeStack(win.oldwin);
        win.close();
    }
    

    uses recursion to close the windows off the stack in order.

    — answered April 10th 2015 by Alex Shah
    permalink
    0 Comments
  • I feel like this may be a more efficient/reusable method than the accepted answer:

    Also, to Alex Shah, why use recursion on something so simple? No need to create so many objects. Just use a loop.

    My approach:

    function toRootPage() {
        while(Ti.UI.currentWindow.oldwin) {
            Ti.UI.currentWindow.close();
        }
    }
    
    — answered August 7th 2015 by Emil Parikh
    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.