Manuel_Rodrigues Manuel_Rodrigues -4 years ago 121
Android Question

close multiple window in titanium

I am opening a new Window when a button is pressed in the current displayed window. When the user clicks back or the back button in android I close my current window and it works fine. But when the user presses the home button of the application to go to the first window, all the other added window will keep there in background, and in android if the user presses the back button of the phone the app goes to the last opened window and not where it should go.

this is how I open a new window:

view.addEventListener('click', function(e) {
var win = Ti.UI.createWindow({
url : 'List.js'
});
win.open();
});


To close the window I use:

Ti.UI.currentWindow.close();


For example I have opened window A,B,C,D and in the D window I press the home button of the app where I must go to the B window. So I need to close the C and D window but I don't now how to close the C window that is in background.

I'm not using Alloy

Answer Source

I believe a way to achieve this is to keep track of the opened windows.

For example, in alloy.js you can have:

Alloy.Globals.openWindows = []

(If your not using Alloy, just use some other global variable)

Now your event handler will look like this:

view.addEventListener('click', function(e) {
    var win = Ti.UI.createWindow({
        url : 'List.js'
    });
    Alloy.Globals.openWindows.append(win);
    win.open();
});

Now if you need close the window and parent windows you can just do this:

Alloy.Globals.openWindows.pop().close()

You can repeat the above line as many times as needed to close the required number of windows. For example if you needed to close 3 parent windows you can just do the following:

for (var i = 0: i < 3; i++) {
    Alloy.Globals.openWindows.pop().close()
}

If you need to close ALL windows, except the first you can do this (assuming the first window was not added to openWindows):

while(Alloy.Globals.openWindows.length > 0) {
    Alloy.Globals.openWindows.pop().close()
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download