How can I close a window that isn't the currentWindow?
This probably sounds like a very simple thing to do, but I'm really struggling, and I've tried all sorts.
Basically the user is shown a menu, they select one of the items and is taken to a form. They fill out the form and it POSTs something to the API on my server that I've built. Upon POST completion the user is shown a third window, which some text on it, a 'Thank you' window. That all works fine.
The issue is that every page has a 'Cancel' or 'Back' button that simply does Ti.UI.currentWindow.close()
, closing the current window and displaying the one that was open before. This is fine for all the other pages on the app apart from the process described above. I want it to close both the current window and the one below it (the form).
I could, of course, just reload the root window above everything, but that just seems to be illogical. I'll be using up more memory than I need to be, and probably end up making more work for myself down the line, especially if the user starts pressing 'Back' buttons elsewhere on the app and then gets shown windows they thought they had closed earlier.
I fear that I haven't grasped how to use windows correctly, but I'm not sure how I could have got it wrong. Please see code snippets below (this isn't the full code, for obvious reasons):
Root window:
var.winAdd = Ti.UI.createWindow({
url:'add.js'
});
winAdd.open();
Add window:
...code for form here...
var btnSubmit = Ti.UI.createButton({
...
});
btnSubmit.addEventListener('click', function(e)
{
if(api request is successful){
var winAdded = Ti.UI.createWindow({
url:'added.js'
});
winAdded.open();
} else {
alert('error...');
}
});
var btnBack = Ti.UI.createButton({
...
});
btnBack.addEventListener('click', function(e)
{
Ti.UI.currentWindow.close();
});
Added window:
... thank you text here ...
var btnBack = Ti.UI.createButton({
...
});
btnBack.addEventListener('click', function(e)
{
Ti.UI.currentWindow.close();
});
3 Answers
-
Mike
Firstly, don't use the js alert() method, use the Ti view that was created for the purpose, which is Ti.UI.AlertDialog.
Also, instead of using Ti.UI.currentWindow.close(), you can simply assign Ti.UI.currentWindow to a variable at the top of each script and perform the close() on that instead:
var win = Ti.UI.currentWindow; win.close();
So, with these suggestions in mind, rather than opening a new window, simply display an AlertDialog, and listen for the click event on a button, which I imagine would be an "OK" button. When it is clicked, (inside that eventListener) close the dialog and close the window. This will return you to the window underneath the current window.
Hope this helps
-
Hi,
Thanks for the response. Although the code above is just to demonstrate what I'm doing, so I don't actually use an alert() anywhere. Having said that, I wasn't aware there was a Ti.UI.AlertDialog, so thanks.
I've tried setting a variable to
Ti.UI.currentWindow
and then closing that, it has the exact same effect as usingTi.UI.currentWindow
straight off, which you'd expect it to.I'm not sure replacing a window with an alertDialog is a viable solution at the moment. Although the window I'm showing only has text and a single button within it, it probably won't do in the future. Ideally I'll have a list of options for the user to select from, as most good UI's generally do.
So, is there a way to close the current window and it's parent?
-
In response to your answer, Mike (which, incidentally, as it is not truly an answer ought to have been posted as a comment response to my answer, to make it easier for people to follow the conversation in future), the approach is the same…
Simply open the window within an event listener, and then on the next line, close the current window. Hence, in your code:
var win = Ti.UI.currentWindow; var btnSubmit = Ti.UI.createButton({ ... }); btnSubmit.addEventListener('click', function(e) { if(api request is successful){ var winAdded = Ti.UI.createWindow({ url:'added.js' }); winAdded.open(); } else { alert('error...'); } //this is the important step: win.close(); }); var btnBack = Ti.UI.createButton({ ... }); btnBack.addEventListener('click', function(e) { win.close(); });
Hope this clarifies my previous answer.