Proper way of opening, closing, and reopening a window

You must Login before you can answer or comment on any questions.

What is the proper way of opening, closing and reopening a window. I currently have it as below:

app.js

win.open();
win.js
win.close();
Called again in app.js
win.open();
When the window is opened the 2nd time, I run into all kinds of errors. Mainly, none of the buttons respond.

— asked 3 years ago by Pete Halatsis
2 Comments
  • im having this bizzarre behavior too.. after certain time, all the button inside a window is not working..

    — commented 2 years ago by Aizil Akmar Omar

  • i think i managed to solve my problem. Before closing any window, just remove all the tableview, buttons, labels, views etc.. and then close the window. It will help Titanium from doing bizarre behaviors like tableview not rendering correctly, buttons not working etc.

    win_about.remove(b_close1);
        win_about.remove(AboutTable);
        win_about.close();

    — commented 2 years ago by Aizil Akmar Omar

3 Answers

when you called win.close(); I believe the framework attempts to clean up things and that is why you are seeing bizarre behavior.

what I usually do is just have a function that creates the window, and it checks to see if window is null before executing.

I am assuming this behavior based on this from the documentation

Optimizations UI objects are optimized by Titanium to not be realized into the drawing context and placed into the device UI surface until needed. That means that you can create UI objects, set their properties and add them to their hierarchy without much worry about memory or performance. When the native drawing surface needs to render a specific view or control, Titanium will automatically create the view as needed. Additionally, Titanium is optimized to also release memory once the view is no longer needed, on screen or in low memory situations. However, it's a good idea to help Titanium along in certain cases where you are no longer using objects. For example, you should call close on a Window instance when you are no longer using it. You can safely call open on the Window again to re-open it.

— answered 3 years ago by Aaron Saunders
answer permalink
4 Comments
  • And if the window is not null? I added something similar and attempted to close the window again with no success, it seems that the window is never fully closing.

    To test my code, I commented out almost everything to test that it is not being called multiple times..

    — commented 3 years ago by Pete Halatsis

  • why close the window if you are going to utilize it again? Just hide it...

    — commented 3 years ago by Aaron Saunders

  • I close the window so that when it is opened, it recalculates my data. I have tried calling them as a function when the window has focus but this does not fire... How should I be reloading the user inputs on window 1 for calculations on window2?

    — commented 3 years ago by Pete Halatsis

  • Show 1 more comment

If I understand well from your last comment, you have a window win1 that presents some information and opens another window win2, where you perform some task that affect the information in win1: when you close win2, data in win1 must be changed accordingly.

If my reconstruction is right, you might set up a custom event listener in win1 and fire a synthesized event in win2, just before closing it. In the event handler you simply update the presented data.

I also experienced the same problem with the 'focus' event not being fired on Android, so I came up with this fallback solution that worked for me.

— answered 3 years ago by Olivier Morandi
answer permalink
3 Comments
  • You have the basic idea of what I am trying to do. I am not much of a programmer so could you give me an example of your suggestion? Thank you.

    — commented 3 years ago by Pete Halatsis

  • Hi Pete, check out some sample code here: https://gist.github.com/747260.

    — commented 2 years ago by Olivier Morandi

  • Thank you for that, though I cannot see how to use it for my case. I am building something similar to a mortgage calculator, data inputed by the user on win1 and calculated/displayed on win2.

    — commented 2 years ago by Pete Halatsis

The body of my program is using scrollview, when I open my win2 the 2nd time, I can scroll the body and see a duplicate of the information behind it. It does seem that the window is never fully closing. Perhaps I could run a while loop to close all instances? How do I check if a window is open (without setting a variable as a bool because I beleive the bool will get changed but the window will not have actually closed...)

— answered 2 years ago by Pete Halatsis
answer permalink
1 Comment
  • I have tried many different ways such as not using a separate window just hiding body1 for body2, and hiding/showing instead of closing. Both of these have brought me more headaches...

    — commented 2 years ago by Pete Halatsis

Your Answer

Think you can help? Login to answer this question!