How to check if a window exists and is visible
I used the Kitchen Sink example to show an activity indicator. But if the user clicks another tab, I would like to check if indWin exists, and if it is, call hideIndicator(). But not sure how to do it. If I just call hideIndicator and indWin doesnt exist, I get a warning.
Here's the code I'm using. Any help is appreciated:
var indWin = null;
var actInd = null;
function showIndicator()
{
// window container
indWin = Titanium.UI.createWindow({
height:150,
width:150
});
// black view
var indView = Titanium.UI.createView({
height:150,
width:150,
backgroundColor:'#000',
borderRadius:10,
opacity:0.8
});
indWin.add(indView);
// loading indicator
actInd = Titanium.UI.createActivityIndicator({
style:Titanium.UI.iPhone.ActivityIndicatorStyle.BIG,
height:30,
width:30
});
indWin.add(actInd);
// message
var message = Titanium.UI.createLabel({
text:'Loading',
color:'#fff',
width:'auto',
height:'auto',
font:{fontSize:20,fontWeight:'bold'},
bottom:20
});
indWin.add(message);
indWin.open();
actInd.show();
};
function hideIndicator()
{
actInd.hide();
indWin.close({opacity:0,duration:500});
};
//
// Add global event handlers to hide/show custom indicator
//
Titanium.App.addEventListener('show_indicator', function(e)
{
//Ti.API.info("IN SHOW INDICATOR");
showIndicator();
});
Titanium.App.addEventListener('hide_indicator', function(e)
{
//Ti.API.info("IN HIDE INDICATOR");
hideIndicator();
});
2 Answers
-
I may have an answer from some tom-foolery based needs between iOS and Droid.
The premise is that I need to know if a Window is established and if so, nuke it (or do whatever you'd like, really).
Give this a whirl to check the existence of a Window in iOS or Droid:
if ( typeof win1 == 'undefined' ) { // Where win1 = Window Object alert('win1 does NOT exist'); // Do nothing or do something here } else { // win1 is not undefined, so let's nuke it win1.close(); // ... or do what you would like here }
HTH
//Jesse
-
Just create a boolean variable which is set to
true
when the window is opened and set tofalse
when it is closed.