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 to close window and its parent

I am accessing an external database and wish to solicit some filter info from the user before fetching the data.

image

Question:

How can I make the click event on Win C send data to Win A AND close Win B?

I tried an event that Win B listens for, then closes. Win B sees the event but does not close.

I tried having the Win A listener close Win B but the screen just goes white and stays there (the back button brings me to Win A with data correctly passed).

— asked May 18th 2010 by rob stevens
  • stack
  • window
0 Comments

5 Answers

  • Agree with @Bryan on both counts, that's the easiest way and there should be something added to Titanium to allow for quickly closing parent windows.

    If you really need it to look like there's a window c you could use views and animation to make it look like there's a window c when in fact it's just a view sliding in. Here's some basic code to show what I mean:

    app.js

    var tabGroup = Titanium.UI.createTabGroup();
    
    var win1 = Titanium.UI.createWindow({  
        url:'window_a.js',
        title:'window a',
        backgroundColor:'#fff'
    });
    
    var tab1 = Titanium.UI.createTab({  
        icon:'KS_nav_ui.png',
        title:'Tab 1',
        window:win1
    });
    
    tabGroup.addTab(tab1);
    
    tabGroup.open();
    
    Titanium.App.addEventListener('event_one', function(e)
    {
        Ti.API.info('app.js: event one, array length = ' + e.data.length);
        win1.passedData = e.data;
    });
    

    window_a.js

    var win = Titanium.UI.currentWindow;
    
    var button1 = Titanium.UI.createButton({
        title:'open window b',
        top:170,
        left:30,
        height:40,
        width:260
    });
    
    button1.addEventListener('click', function()
    {
        var new_win = Ti.UI.createWindow({
            url:'window_b.js',
            backgroundColor:'#fff'
        });
        Ti.UI.currentTab.open(new_win);
    });
    
    win.addEventListener('focus', function()
    {
        Ti.API.info('win.passedData = ' + win.passedData);
    });
    
    win.add(button1);
    

    window_b.js

    var win = Titanium.UI.currentWindow;
    
    var button1 = Titanium.UI.createButton({
        title:'open window c & do some stuff',
        top:170,
        left:30,
        height:40,
        width:260
    });
    
    button1.addEventListener('click', function()
    {
        view1.animate({left:-320});
        view2.animate({left:0});
        label2.text = 'doing some stuff';
        setTimeout(function()
        {
            label2.text = 'done! passing data & closing'
            setTimeout(function()
            {
                Titanium.App.fireEvent('event_one',{data:['1','2','3']});
                win.close();
            },1700);
        },1700);
    });
    
    var view1 = Ti.UI.createView({
        backgroundColor:'transparent',
        width:320,
        left:0
    });
    
    view1.add(button1);
    
    var view2 = Ti.UI.createView({
        backgroundColor:'transparent',
        width:320,
        left:320
    });
    
    var label2 = Ti.UI.createLabel({
        text:'doing some stuff',
        textAlign:'center'
    });
    
    view2.add(label2);
    
    win.add(view1);
    win.add(view2);
    

    A function in app.js passes data to window_a but could thats just one way to do it.

    — answered May 18th 2010 by Dan Giulvezan
    permalink
    0 Comments
  • I got this to work using an custom event listener on Android. If you try this on iPhone, you will end up with a blank parent window as an open event fires again on iPhone when the window regains focus.

    We finally decided to just combine the two windows into one which worked out better in our case.

    They need to add some sort of window/navigation handler so this is not such a huge pain. By the way, there is a getParent() function, but calling the .close() of getParent() closes the current window.

    — answered May 18th 2010 by Vinh Bui
    permalink
    0 Comments
  • Thanks guys, I think the single window plan is probably best. I wanted my iPhone app to work like my BlackBerry and Android (non-Ti) apps, but that is not a must.

    — answered May 18th 2010 by rob stevens
    permalink
    0 Comments
  • is there a workaround since that time? I want to close a window as soon as my second window is opened on iphone but always ending with a white window/blank parent window.
    Even using setTimeout(closeMe, 10000) does not work. In such a case the blank parent window is displayed 10 seconds later.
    I want to close the window to free memory.

    — answered July 19th 2010 by Jicks Steen
    permalink
    0 Comments
  • I've ran into a similar situation, and my workaround resulted in something like the code shown below.

    bt.addEventListener("click", function(){
        var grandParent = win.parentWindow.parentWindow;
        grandParent.fireEvent("myEvent", {
            data: "test data passed from windowC!"
        });
        var parent = win.parentWindow;
        Ti.UI.currentTab.close(parent);
        Ti.UI.currentTab.close(win);
    });
    

    The key is to close the parent window (windowB) by calling Ti.UI.currentTab.close before closing the last window (windowC).

    tested on iPhone, with Titanium SDK 1.8.0.1

    — answered February 1st 2012 by Akinori Takigawa
    permalink
    0 Comments
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.