I had a customer that was wondering how they could close all the popup windows in one fell swoop. They knew they could keep track of each popup they created in say an array, then when they wanted to close them all, they could just iterate the array and close each one, but there must be a better way. And there is!
The code and sample below show how to do this. Check the code comments for some interesting details.
-
-
-
<mx:application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical">
-
viewSourceURL="wp-content/code/Flex2.0.1hf2/739stl_CloseAllPopUps/srcview/index.html">
-
<mx:script>
-
<!–[CDATA[
-
import mx.managers.PopUpManager;
-
import mx.managers.PopUpManagerChildList;
-
-
private function openPopup(popUpScope:String):void{
-
var pop
opup = Popup(PopUpManager.createPopUp(this,Popup,false,popUpScope)); -
}
-
-
private function closeAll():void{
-
// if you scope your popups to PopUpManagerChildList.POPUP
-
// this is all you should have to check to clear all popups
-
while(systemManager.popUpChildren.numChildren > 0){
-
PopUpManager.removePopUp(Popup(systemManager.popUpChildren.getChildAt(0)));
-
}
-
// if you scope your popups to other than PopUpManagerChildList.POPUP
-
// you need to scan this and check the class name to decide if you need to remove the child
-
for (var i:int = systemManager.numChildren-1;i>=0;i–){
-
trace(getQualifiedClassName(systemManager.getChildAt(i)));
-
if(getQualifiedClassName(systemManager.getChildAt(i))==‘Popup’){
-
systemManager.removeChildAt(i);
-
}
-
}
-
}
-
-
]]–>
-
</mx:script>
-
<mx:button label="open popup – PopUpManagerChildList.POPUP" click="openPopup(PopUpManagerChildList.POPUP)">
-
<mx:button label="open popup – PopUpManagerChildList.APPLICATION" click="openPopup(PopUpManagerChildList.APPLICATION)">
-
<mx:button label="open popup – PopUpManagerChildList.PARENT" click="openPopup(PopUpManagerChildList.PARENT)">
-
-
<mx:button label="close all" click="closeAll()">
-
-
</mx:button>
-
</mx:button></mx:button></mx:button></mx:application>
Browse the source of this example.
Download a zipfile containing the source to this sample.
This movie requires Flash Player 8
Here is the simple popup that I use:
-
-
-
<mx:titlewindow xmlns:mx="http://www.adobe.com/2006/mxml">
-
width="400" height="300" x="100" y="100" showCloseButton="true" >
-
-
<mx:text text="PopUp">
-
-
</mx:text>
-
</mx:titlewindow>
Tweet
3 Comments »

June 23rd, 2008 at 2:17 am
Thanks for this post.
June 26th, 2008 at 6:05 am
Hmm..
Cool solution.
My two cents on this..
1.I dont get anything in systemManager.popUpChildren if I dont scopt it while adding the popup. (Not many people use that argument at all)
2. How about having the parent dispatch an event and have popups listening to them? I think this gives flexiblity for the popups to do something before they close ‘em selves!
Thanks for the post anyways
June 3rd, 2010 at 1:07 pm
public function closeAllPopup():void
{
var systemManager:SystemManager = Application.application.systemManager
var childList:IChildList = systemManager.rawChildren
for (var i:int=childList.numChildren-1; i >=0; i– )
{
var childObject:* = childList.getChildAt(i)
if (childObject is UIComponent)
{
var uiComponent:UIComponent = childObject as UIComponent
if (uiComponent.isPopUp)
{
PopUpManager.removePopUp(uiComponent)
}
}
}
}