The problem:
When closing a window programmatically in a Navigation group, only the first window in the stack (or last, depending how you look at it) fires the close event, the rest of the windows do not.
The steps to repro
1. Run the following code in app.js
2. Open a few windows with the "open" button
3. Click on the close all button
4. Look at the logs, only the last window fired the "close" event
The code:
function NavGroup() {
var win = Ti.UI.createWindow();
var nav = Ti.UI.iPhone.createNavigationGroup();
var allWindows = [];
win.add(nav);
function open(_window) {
if(_window) {
nav.open(_window);
allWindows.push(_window);
} else {
win.open();
}
}
function close(_window) {
if(_window) {
nav.close(_window);
for(var i = 0, len = allWindows.length; i < len; i++) {
if(_window == allWindows[i]) {
allWindows.splice(i, 1);
break;
}
}
} else {
win.open();
}
}
function setWindow(_window) {
nav.window = _window;
}
function closeAll() {
for(var i = 0, len = allWindows.length; i < len; i++) {
nav.close(allWindows[i]);
}
}
return {
open: open,
close: close,
closeAll:closeAll,
setWindow: setWindow
}
}
function Window(num){
var win = Ti.UI.createWindow({
backgroundColor: '#ccc',
title: 'Window #'+num
});
var btnOpen = Ti.UI.createButton({
title: 'open next',
top: 10,
width: Ti.UI.SIZE,
height: Ti.UI.SIZE
});
var btnCloseAll = Ti.UI.createButton({
title: 'close all',
top: 60,
width: Ti.UI.SIZE,
height: Ti.UI.SIZE
});
var btnCloseThis = Ti.UI.createButton({
title: 'close this window',
top: 110,
width: Ti.UI.SIZE,
height: Ti.UI.SIZE
});
win.add(btnOpen);
win.add(btnCloseAll);
if(num !== 1)
win.add(btnCloseThis);
btnOpen.addEventListener('click', function(){
num++;
Nav.open(Window(num));
});
btnCloseAll.addEventListener('click', function(){
Nav.closeAll();
});
btnCloseThis.addEventListener('click', function(){
Nav.close(win);
});
win.addEventListener('close', function(){
Ti.API.info('Closing Window #' + num)
});
return win;
}
var Nav = NavGroup();
Nav.setWindow(Window(1));
Nav.open();
Expanded test case
function NavGroup() { var win = Ti.UI.createWindow(); var nav = Ti.UI.iPhone.createNavigationGroup(); var allWindows = []; win.add(nav); function open(_window) { if(_window) { nav.open(_window); allWindows.push(_window); } else { win.open(); } } function removeWin(_window) { for(var i = 0, len = allWindows.length; i < len; i++) { if(_window == allWindows[i]) { allWindows.splice(i, 1); break; } } } function close(_window,opts) { if(_window) { nav.close(_window,opts); } else { win.open(); } } function setWindow(_window) { nav.window = _window; } function closeAll() { for(var i = 0, len = allWindows.length; i < len; i++) { nav.close(allWindows[i]); } } function closeAll2(opts) { for(var len = allWindows.length, i = len-1 ; i >=0 ; i--) { nav.close(allWindows[i],opts); } } return { open: open, close: close, closeAll:closeAll, closeAll2:closeAll2, setWindow: setWindow, removeWin:removeWin } } function Window(num){ var win = Ti.UI.createWindow({ backgroundColor: '#ccc', title: 'Window #'+num, number:num }); var btnOpen = Ti.UI.createButton({ title: 'open next', top: 10, width: Ti.UI.SIZE, height: Ti.UI.SIZE }); var btnCloseThis = Ti.UI.createButton({ title: 'close this (animated)', top: 60, width: Ti.UI.SIZE, height: Ti.UI.SIZE }); var btnCloseThis2 = Ti.UI.createButton({ title: 'close this', top: 110, width: Ti.UI.SIZE, height: Ti.UI.SIZE }); var btnCloseAll = Ti.UI.createButton({ title: 'close All (Back->Front)', top: 160, width: Ti.UI.SIZE, height: Ti.UI.SIZE }); var btnCloseAll2 = Ti.UI.createButton({ title: 'close All (Front->Back animated)', top: 210, width: Ti.UI.SIZE, height: Ti.UI.SIZE }); var btnCloseAll3 = Ti.UI.createButton({ title: 'close All (Front->Back)', top: 260, width: Ti.UI.SIZE, height: Ti.UI.SIZE }); win.add(btnOpen); if(num !== 1) { win.add(btnCloseThis); win.add(btnCloseThis2); win.add(btnCloseAll); win.add(btnCloseAll2); win.add(btnCloseAll3); } btnOpen.addEventListener('click', function(){ num=win.number+1; Nav.open(Window(num)); }); btnCloseAll.addEventListener('click', function(){ Nav.closeAll(); }); btnCloseAll2.addEventListener('click', function(){ Nav.closeAll2({animated:true}); }); btnCloseAll3.addEventListener('click', function(){ Nav.closeAll2({animated:false}); }); btnCloseThis.addEventListener('click', function(){ Nav.close(win,{animated:true}); }); btnCloseThis2.addEventListener('click', function(){ Nav.close(win,{animated:false}); }); win.addEventListener('close', function(){ Ti.API.info('Closing Window #' + win.number) Nav.removeWin(win); }); return win; } var Nav = NavGroup(); Nav.setWindow(Window(1)); Nav.open();Pull pending https://github.com/appcelerator/titanium_mobile/pull/2909
Vishal - Please let me know when this gets put into one of the nightly releases.
Closing as fixed. Tested and verified on: Titanium Studio, build: 3.0.0.201210220122 Titanium SDK, build: 3.0.0.v20121025171611 Device: iPhone 5 (6.0)