In certain cases your application needs to be notified when the user is closing the window.
No event will be dispatched by Flash Player or Flex framework when the user is closing the window. Your application can still be notified via JavaScript using ExternalInterface API.
No event will be dispatched by Flash Player or Flex framework when the user is closing the window. Your application can still be notified via JavaScript using ExternalInterface API.
All you have to do is have a JavaScript function which will be invoked when the HTML page is unloaded. In this JavaScript function you can invoke a function in Flex to perform your desired operation.
First add the JavaScript code below to application HTML wrapper
<SCRIPT LANGUAGE="JavaScript">
window.onbeforeunload = callFlexCleanUp;
function callFlexCleanUp()
{
var flexReference = document.${application} || window.${application};
flexReference.flexCleanUp();
}
</SCRIPT>
Your Flex application should like the one below
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical"
creationComplete="initApp()">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
private function initApp():void
{
ExternalInterface.addCallback("flexCleanUp",doTheCleanUp);
}
private function doTheCleanUp():void
{
trace("Cleaned");
}
]]>
</mx:Script>
</mx:Application>
Note:
1. This is for Flex applications running in a browser. Flex applications running in AIR will dispatch a event when the application is being closed.
2. In your ActionScript function which is executed when the browser is closed by the user, any requests sent to the server might not be completed. This is because the browser in which the application is running might not complete the requests as the browser is closing off. This behavior seems to be found with Internet Explorer.