Post updates:
- [December 19th, 2011] added information in the source code to use the PopUpUtils class in Flex 3.x and lower or Flex 4.x and higher (thanks to FlexMaster_Flash for the feedback in the comments)
In Flex, you can create popups easily, but what if you want to get all of them after they’ve already been created? You could keep track of every created popup using a customized function that puts every single instance in an array for example, but what if you’re using a Flex component that simply creates popups in its own way and you cannot keep track of what it creates? Here I’m going to provide a possible solution.
After some debugging, I found out that Flex puts all the popups among the rawChildren in the application’s SystemManager. This is where we can find them. So now that we know where to look, we need a way to distinguish the children that are popups from those that are not (rawChildren does not contain only popups). If we use only components whose base class is the standard UIComponent class (I think this is the case most of the times), there’s an easy way to know if an instance is a popup or not: the isPopUp property of UIComponent.
Whenever we create a new popup that extends UIComponent, the PopUpManager sets the isPopUp property to true. So, all we need to do is loop through the application’s systemManager.rawChildren and check whether we find an UIComponent instance whose isPopUp property is true. In that case, we’ve got an application’s popup.
To demonstrate the concept, I’ve implemented the PopUpUtils class. It simply allows you to get all the popups inside an application instance, to discover if there are visible popups and to close all the popups. Let’s take a look at it.
package com.devahead.utils
{
import flash.display.DisplayObject;
import mx.collections.ArrayCollection;
import mx.core.Application;
//import mx.core.FlexGlobals; // NOTE: use this import for Flex 4.x and higher
import mx.core.IChildList;
import mx.core.UIComponent;
import mx.managers.PopUpManager;
public class PopUpUtils
{
/**
* Returns all the popups inside an application. Only the popups whose base
* class is UIComponent are returned.
*
* @param applicationInstance
* Application instance. If null, Application.application is used.
* @param onlyVisible
* If true, considers only the visible popups.
* @return All the popups in the specified application.
*/
public static function getAllPopups(applicationInstance: Object = null,
onlyVisible: Boolean = false): ArrayCollection
{
var result: ArrayCollection = new ArrayCollection();
if (applicationInstance == null)
{
// NOTE: use this line for Flex 4.x and higher
//applicationInstance = FlexGlobals.topLevelApplication;
// NOTE: use this line for Flex 3.x and lower
applicationInstance = Application.application;
}
var rawChildren: IChildList = applicationInstance.systemManager.rawChildren;
for (var i: int = 0; i < rawChildren.numChildren; i++)
{
var currRawChild: DisplayObject = rawChildren.getChildAt(i);
if ((currRawChild is UIComponent) && UIComponent(currRawChild).isPopUp)
{
if (!onlyVisible || UIComponent(currRawChild).visible)
{
result.addItem(currRawChild);
}
}
}
return result;
}
/**
* Checks if an application has visible popups. Only the popups whose base
* class is UIComponent are considered.
*
* @param applicationInstance
* Application instance. If null, Application.application is used.
* @return True if there are visible popups in the specified application,
* false otherwise.
*/
public static function hasVisiblePopups(applicationInstance: Object = null): Boolean
{
if (applicationInstance == null)
{
// NOTE: use this line for Flex 4.x and higher
//applicationInstance = FlexGlobals.topLevelApplication;
// NOTE: use this line for Flex 3.x and lower
applicationInstance = Application.application;
}
var rawChildren: IChildList = applicationInstance.systemManager.rawChildren;
for (var i: int = 0; i < rawChildren.numChildren; i++)
{
var currRawChild: DisplayObject = rawChildren.getChildAt(i);
if ((currRawChild is UIComponent) && UIComponent(currRawChild).isPopUp
&& UIComponent(currRawChild).visible)
{
return true;
}
}
return false;
}
/**
* Closes all the popups belonging to an application. Only the popups
* whose base class is UIComponent are considered.
*
* @param applicationInstance
* Application instance. If null, Application.application is used.
* @return The list of the closed popups.
*/
public static function closeAllPopups(applicationInstance: Object = null): ArrayCollection
{
var allPopups: ArrayCollection = getAllPopups(applicationInstance);
for each (var currPopup: UIComponent in allPopups)
{
PopUpManager.removePopUp(currPopup);
}
return allPopups;
}
}
}
As you can see, the getAllPopups function returns all the popups inside an appplication instance. If the instance is not specified, then the current application is used (the one in which the function is called). The hasVisiblePopups is basically the same, except the fact that it stops executing as soon as a visible popup is found. The closeAllPopups function closes all the popups in the application.
Here is an example on how the PopUpUtils class can be used.
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
pageTitle="Getting all the popups in a Flex application - devahead BLOG"
layout="vertical" horizontalAlign="left"
applicationComplete="{onApplicationComplete(event)}"
viewSourceURL="srcview/index.html">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.core.UIComponent;
import mx.collections.ArrayCollection;
import com.devahead.utils.PopUpUtils;
import mx.core.IFlexDisplayObject;
import mx.containers.Panel;
import mx.managers.PopUpManager;
import mx.events.FlexEvent;
protected var counter: int = 1;
protected function onApplicationComplete(event: FlexEvent): void
{
// Add 10 popups on startup
for (var i: int = 0; i < 10; i++)
{
addPopUp();
}
}
protected function addPopUp(): void
{
// Create a Panel popup setting a random position for it
const X_OFFSET: int = 200;
var popup: IFlexDisplayObject = PopUpManager.createPopUp(this, Panel);
popup.width = 200;
popup.height = 200;
popup.x = X_OFFSET + (Math.random() * (width - X_OFFSET - popup.width));
popup.y = Math.random() * (height - popup.height);
(popup as Panel).title = "PopUp " + (counter++);
}
]]>
</mx:Script>
<mx:Button label="Add popup" click="{addPopUp()}"/>
<mx:Button label="Are there visible popups?"
click="{Alert.show(PopUpUtils.hasVisiblePopups() ? 'Yes' : 'No',
'Are there visible popups?')}"/>
<mx:Button label="Close all popups" click="{PopUpUtils.closeAllPopups()}"/>
</mx:Application>
The example application creates 10 popups on startup, then you can create additional popups, you can check if there are visible popups or you can close them all. I used a standard Panel to create popups instances. The X_OFFSET is useful only to avoid having the popups over the buttons, just for usability.

November 23rd, 2010 at 06:28
Hi,
I want to detect whether a popup is modal or not. If it is modal, then the rest of the components will be blurred and the user cannot interact with them. I want to programmatically catch this scenario.
Could you please tell me how?
Thanks,
Pradeep.
November 28th, 2010 at 22:40
Hi Pradeep.
I don’t know an easy way to detect if a popup is modal or not as the modal windows management seems to be internal to mx.managers.PopUpManagerImpl in the Flex framework. If you look at the createModalWindow function in that class, you’ll see that the modal windows could be created as FlexSprite named modalWindow and they are always the child immediately before the corresponding popup in systemManager.rawChildren. So if this is the case for you, you could just go through the systemManager.rawChildren as the function getAllPopups in PopUpUtils does and check if the raw child before the popup is named modalWindow to see if it’s modal. I hope this helps.
January 18th, 2011 at 03:31
Thanks for the post! Saved me a bunch of time. I owe you a beer
February 18th, 2011 at 03:12
Hey Pradeep,
Its pretty straight forward with flex 3.5 sdk you can access a property called nummodelwindows its gives the list of model windows currently in ur application and then play with it..
March 3rd, 2011 at 08:05
thanks for posting!
March 23rd, 2011 at 14:37
I was using PopUpManagerChildList to track popups in order to remove the all in one go, but I found that it crashed my application when one of the popups contained a MenuBar, which also uses the PopUpManager.
I used your example here to write a new method for finding and removing all of the popups without using PopUpManagerChildList.
Thank you.
June 8th, 2011 at 00:40
I Love you ! you saved my day ..
December 18th, 2011 at 01:46
Thanks for this post, you saved my day as well!
One note: got a warning that “Application” had been deprecated, so the following…
applicationInstance = Application.application;
needs to change to…
applicationInstance = FlexGlobals.topLevelApplication;
..and of course you will need this as well:
import mx.core.FlexGlobals;
With those changes, works perfectly in Flash Builder 4.0.1
December 19th, 2011 at 22:18
Thanks for the feedback FlexMaster_Flash! You’re right. This post was pretty old. I’ve just updated it.