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.
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.
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;
}); -
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.
-
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.