Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

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();
});
— asked November 18th 2010 by Mike Griffiths
  • android
  • iphone
  • management
  • mobile
  • window
0 Comments

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

    — answered November 18th 2010 by Paul Dowsett
    permalink
    0 Comments
  • 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 using Ti.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?

    — answered November 18th 2010 by Mike Griffiths
    permalink
    0 Comments
  • 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.

    — answered November 18th 2010 by Paul Dowsett
    permalink
    3 Comments
    • Thanks for the answer. Thanks for pointing out the fact I should be posting as comments - didn't even realise, apologies!

      I've tried the above code - I'd actually tried this before posting as I found it in another thread - this gives me the same outcome as I originally had:

      • The new window opens
      • The new window closes a second or so later automatically
      • I get returned to the original window (the one that should be closed)

      Any help is appreciated. Thanks

      — commented November 19th 2010 by Mike Griffiths
    • Hi Mike. There is no apologies necessary about the comments. It's not clearly explained anywhere, and I too did it wrong to begin with. :)

      I will happily help you with your question, but you will need to post a script that works without modification when I paste it into a new project's app.js. Please remove as much code as you can until it just simply exhibits the behaviour. Then we can take from there.

      Thanks

      — commented November 19th 2010 by Paul Dowsett
    • I hate to reopen old threads, but I'm having this exact same issue. Trying to close the current window and open a new window results in the newly opened window closing after a second or so along with the window that I wanted to close.
      Did you ever reach a solution?

      — commented March 1st 2012 by Nathaniel Mitchell
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.