I've seen a number of questions concerning how to do some type of
shutdown handling in flex.  It's a well known fact that Flex doesn't
have any type of standard event that fires when Flash is exiting.  My
current project needed some cleanup routines run if the user closed
the browser or navigated away from the page.  Here is what I came up with.

In the html page that loads your flex app, add the following
javascript block.

<!-- Andrew's special code to allow shutdown handlers in Flex -->
<script language="JavaScript" type="text/javascript">
        function shutdownHook()
        {
                ${application}.flexShutdownHandler();
        }
        
        //Set up notification for flex app when page unloads
        window.onbeforeunload=shutdownHook;
</script>

Then in your flex app's init method (usually called from
creationComplete), expose the handler function to JavaScript using
ExternalInterface.

private function init():void
{
        //set up the shutdown hook function
        if(ExternalInterface.available)
                ExternalInterface.addCallback("flexShutdownHandler",
flexShutdownHandler);
}

private function flexShutdownHandler():void
{
        trace("perform shutdown code here");                            
}

Hope this helps those in need.
-Andrew Westberg

Reply via email to