Catch close/unload event in Actionscript/Flex application

actionscript-3airapache-flexflash

This questin might sound very, very simplistic, yet I have not been able to find an answer, neither on official documentation nor official forums or anywhere.

I am just looking for the event fired when the user closes the application. This event MUST exist, Flash has been around for 11 versions now, I could not imagine there is no such thing…

Thanks in advance

Best Answer

Since you're looking for an AIR specific solution; you'll want to take listen for the closing event on your main application tag ( WindowedApplication).

Conceptually something like this:

<s:WindowedApplication closing="onClosing(event)">
 <fx:Script>
   public function onClosing(event:Event):void{
     // execute code here
   }
 </fx:Script>

</s:WindowedApplication>

You may also examine the close event on the same class, or the exiting event on the NativeWindow class. You can access the NativeWindow using something like this:

<s:WindowedApplication creationCompete="onCreationComplete(event)">
 <fx:Script>
   public function onCreationComplete(event:Event):void{
     this.addEventListener('exiting',onExiting);
   }
   public function onExiting(event:Event):void{
     // execute code here
   }
 </fx:Script>

</s:WindowedApplication>
Related Topic