Search code examples
javascripttitaniumappceleratortitanium-alloy

Titanium & Alloy - temporary window closes entire app when killed


I have 2 windows open, a main one; and a "popup" loading window that should show only for a few moments.

When i try to

loading.close();

the loading screen, ALL windows get closed immediately. No error code or anything, the app just straight up closes as there are no more views/windows on screen. But i have never closed the main "index" view, so i do not understand why it disappears. My main idea was to use a

loading.hide();

in order to hide the loading screen and allow the rest to continue, but when the hide executes, the app does not fire or respond to any event until manually forced close.


A litte more code:

index.js

var loading = Alloy.createController("loading").getView();

function updateMainMenu() {
    if (Ti.App.dataReady){  

        //Actually create the main menù, only if data is available.
        //Cut out of snippet due to sheer length.
        //When menù is complete, close the loading window.

        loading.close();  //##### THIS CLOSES ENTIRE APP #####
    }            
}

//##### IF LOADING IS HIDDEN INSTEAD, EVENTS NEVER TRIGGER #####
//When a list element is clicked open the corresponding page.  
function onListClick(e) {
    var win = Alloy.createController('controlPage', e.row.data).getView();
    win.open();
}

//Loads the View, displays it and keeps it updated.
$.index.open(); 
loading.open();
setInterval(updateMainMenu, 2000); 

loading.js/tss/xml has no relevant code for this problem, or at least i think so. It only has a window and an imageView with an image changing every half-second, and it works perfectly until closed.

Notes:

  • Global variable Ti.App.dataReady comes from a background task (another file, works correctly);
  • Event onListClick is bound correctly and works (if i do not open the loading in the first place)

Solution

  • First of all, try not to use index.open as this refers to the "file" and not the actual window. Make sure to use an id in the tss file for the window and use that .open(), would like to see your complete index controller to give more accurate answer.

    Then you open two windows at the same time. Trying to open two windows in a row like this will probably give you a race condition. As mentioned make sure your first window is open before you open any overlay window.

    A good advice, since you are making cross platform apps here is to make things sure (windows are different for all platforms, iOS, Android and Windows). For Android the first window is critical and will be the root window. If by any chance your overlay window is finished first (and yes some things are asynchronous) then your overlay is root, closing it WILL close your app according to Android docs.

    SO, always make sure your first window is "done" before opening anything else. listen for open event and then open overlay.

    Now if you want it to look nice, you may want to specify the main window with two views instead in the XML file. start with a "hide" on your list window and a "show" on your overlay. That way you can shift anytime you like. You can then reuse the overlay "progress" window if you like.

    In my apps I use a view in a widget so I do not have to use a show/hide, that way I can also reuse it. I guess it is a flavor question (unless we start talking memory footprint)

    Hope this makes sense for you...