简体   繁体   中英

Catch close/unload event in Actionscript/Flex application

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

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>

Well, think about it for a minute. You're asking for an event, to be fired, when the application closes... so, what fires the event, and what's going to handle it? The application? But it just closed. You could of course, do something in javascript and check the status of the flash embed, but even that isn't always going to work depending on how the flash player is being unloaded (EG the user closes their browser). Furthermore, what happens if the user kills the flash sandbox or browser? There's no way to send an event from a terminated application.

Ultimately I'd take a very close look at exactly what it is you're trying to accomplish and try to figure out a better way of doing it. If you're worried about a remote resource like a server cleaning up some resources, you might want to implement a keepalive signal and just do your cleanup when you stop receiving it.

Please have a look at Closing event You can attach event listener to it and in callback function use prevent default so window doesn't get closed and you can run your logic. After that by calling close() function, you can close your window.

So you know for sure your what you want to run at time of closing get executed successfully.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM