October 4th, 2007 by Kyle
Posted in: Flex
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:Popup = 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:Application>
Browse the source of this example.
Download a zipfile containing the source to this sample.
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:TitleWindow>





Recent Comments