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.
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.
-
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.
-
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.
-
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.
-
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(); } }