Stopping an AIR app window closing with Flash IDE AS3

If you’re doing document manipulation or something you might need to confirm with the user of the app that they want to quit the app before letting them quit.

To do this, there’s plenty of help online if using Flex, but if writing AIR using the Flash IDE, it’s slightly different.

1. Import the NativeWindow and NativeApplication classes.

import flash.display.NativeWindow;
import flash.desktop.NativeApplication;

2. Set your window instance.

public var window:NativeWindow; 

3. set the window instance – this is a reference to the desktop window the AIR app is in.

window = this.stage.nativeWindow; 

4. Add an Event.CLOSING listener to the window instance.

window.addEventListener(Event.CLOSING, exitingHandler);

5. In the handler for that event, go ‘e.preventDefault()’ to stop the window closing. After that point, you need to manually call NativeApplication.nativeApplication.exit(); to exit the module.

/// runs when the app is being closed
private function exitingHandler(e:Event):void 
{
e.preventDefault();
Utils.confirm("Are you sure you want to exit?", closeHandler);
}

/// closes the app.
private function closeHandler():void
{
trace("CLOSE!");
NativeApplication.nativeApplication.exit();

}

AIR is awesome.

Tech Reference: ,

Leave a Reply

Your email address will not be published. Required fields are marked *

Proudly powered by WordPress | Theme: Baskerville 2 by Anders Noren.

Up ↑