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
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)