Titanium Community Questions & Answer Archive

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

Reloading Window Causes Duplicate Display

I am building an app that requires fresh data from an API each time a window is touched. To force fresh data, I tried creating the display in the window focus event (see simplified code below).

Ti.UI.currentWindow.addEventListener('focus', function()
    {
        var timeStamp = new Date();

        var win9 = Ti.UI.currentWindow;
        win9.backgroundColor = '#fff';


        var lblDate = Titanium.UI.createLabel({
                text:timeStamp
            });
        win9.add(lblDate);
    });

This creates the window perfectly the first time. However, when I return to the window after it has initially been created, the app simply writes the new data on top of the existing window.

Reload issue

This makes sense to me since I'm using Ti.UI.currentWindow. However, is there any way to clear the contents of a window? I'm picturing something like Ti.UI.currentWindow.clear(). If this is not possible, is there a workaround that I'm missing?

I feel confident that I am overlooking something obvious.

Thanks in advance for your help.

— asked April 5th 2010 by JC Yandell
  • 1.1
  • focus
  • mobile
  • refresh
  • reload
  • titanium
0 Comments

3 Answers

  • You just need to update the text on the refresh, put the rest of the code on the base page so it loads just the once.

    var win9 = Ti.UI.currentWindow;
    win9.backgroundColor = '#fff';
    var lblDate = Titanium.UI.createLabel({});
    win9.add(lblDate);

    Ti.UI.currentWindow.addEventListener('focus', function()
    {
    var timeStamp = new Date();
    lblDate.text = timeStamp;
    });

    — answered April 5th 2010 by Andrew Henshaw
    permalink
    0 Comments
  • Thanks for your help. Andrew, I had tried your solution and the label did not print. I just discovered that you are required to set the text color in order for it to display. For those following at home, this did the trick.

    var win9 = Ti.UI.currentWindow;
    win9.backgroundColor = '#fff';
    var lblDate = Titanium.UI.createLabel({color:'#000'});
    win9.add(lblDate);
    
    Ti.UI.currentWindow.addEventListener('focus', function() { 
        var timeStamp = new Date();
        lblDate.text = timeStamp;
        });
    

    Thanks again for your help.

    — answered April 5th 2010 by JC Yandell
    permalink
    0 Comments
  • You can remove the old controls and then add the new ones. It's better if you just update data in existing controls if possible.

    — answered April 5th 2010 by Don Thorp
    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.