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.

  1.  
  2.  
  3. <mx:application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical">
  4.         viewSourceURL="wp-content/code/Flex2.0.1hf2/739stl_CloseAllPopUps/srcview/index.html">
  5.         <mx:script>
  6.                 <!–[CDATA[
  7.                         import mx.managers.PopUpManager;
  8.                         import mx.managers.PopUpManagerChildList;
  9.  
  10.                         private function openPopup(popUpScope:String):void{
  11.                                 var pop :P opup = Popup(PopUpManager.createPopUp(this,Popup,false,popUpScope));
  12.                         }
  13.  
  14.                         private function closeAll():void{
  15.                                 // if you scope your popups to PopUpManagerChildList.POPUP
  16.                                 // this is all you should have to check to clear all popups
  17.                                 while(systemManager.popUpChildren.numChildren > 0){
  18.                                         PopUpManager.removePopUp(Popup(systemManager.popUpChildren.getChildAt(0)));
  19.                                 }
  20.                                 // if you scope your popups to other than PopUpManagerChildList.POPUP
  21.                                 // you need to scan this and check the class name to decide if you need to remove the child
  22.                                 for (var i:int = systemManager.numChildren-1;i>=0;i–){
  23.                                         trace(getQualifiedClassName(systemManager.getChildAt(i)));
  24.                                         if(getQualifiedClassName(systemManager.getChildAt(i))==‘Popup’){
  25.                                                 systemManager.removeChildAt(i);
  26.                                         }
  27.                                 }
  28.             }
  29.  
  30.                 ]]–>
  31.         </mx:script>
  32.         <mx:button label="open popup – PopUpManagerChildList.POPUP" click="openPopup(PopUpManagerChildList.POPUP)">
  33.         <mx:button label="open popup – PopUpManagerChildList.APPLICATION" click="openPopup(PopUpManagerChildList.APPLICATION)">
  34.         <mx:button label="open popup – PopUpManagerChildList.PARENT" click="openPopup(PopUpManagerChildList.PARENT)">
  35.  
  36.         <mx:button label="close all" click="closeAll()">
  37.  
  38. </mx:button>
  39. </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:

  1.  
  2.  
  3. <mx:titlewindow xmlns:mx="http://www.adobe.com/2006/mxml">
  4.         width="400" height="300" x="100" y="100" showCloseButton="true" >
  5.  
  6.         <mx:text text="PopUp">
  7.  
  8. </mx:text>
  9. </mx:titlewindow>
Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Add to favorites
  • LinkedIn
  • Reddit
  • RSS
  • Slashdot
  • Technorati
  • Tumblr
  • Twitter



3 Comments »