开发者

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 th开发者_StackOverflowing...

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 (E.G. 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜