Open New or Activate Existing Flex Window in Adobe AIR Application by Searching WindowedSystemManager for nativeApplication.openedWindows

You have an Adobe AIR native application and you want to activate a Window for a particular mxml class if it exists or open a new one if it does not, right?

For example: If you have a configuration dialog opened by a menuitem or a button, you don’t want to open another instance of that dialog if it’s still open (and, obviously, it’s not modal).

In an AIR application, NativeApplication.nativeApplication.openedWindows contains a list of the NativeWindow instances opened by the application.

NativeWindow instances contain the Window class on their respective stage stacks.

This function seems to work well for me:

function windowOpenOrActivateByClass( myClass:Class ): Object
{
for each( var win:NativeWindow in NativeApplication.nativeApplication.openedWindows )
{
for ( var i:int = 0; i < win.stage.numChildren; i++ )
{
var mgr:WindowedSystemManager = win.stage.getChildAt(i) as WindowedSystemManager;

if ( mgr == null )
continue;

for ( var j:int = 0; j < mgr.numChildren; j++ )
{
var obj:Object = mgr.getChildAt(j);
if ( obj is myClass )
{
obj.activate();
return obj;
}
}
}
}

obj = new myClass();
obj.open();
return obj;
}

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.