Closing all your application windows in AIR

Here’s a little snippet of code I sometimes use in AIR applications to make sure all windows close when the main application window closes. I usually put it in the main application MXML file (the main window of the application). It stops the main window from closing, and closes any other opened application windows first in the opposite order in which they were opened (more recent windows first). Without code like this, if you have any other windows open, even little utility windows, and your main application window is closed, your application will not exit, even if NativeApplication.autoExit is set to true.

for (var i:int = NativeApplication.nativeApplication.openedWindows.length - 1; i >= 0; --i)

5 Responses to Closing all your application windows in AIR

  1. I’m kinda surprised there isn’t some built in API for this. Maybe a good idea for the next point release?

  2. paravoice says:

    If you want wo close all the windows,why not invoke the NativeApplication.nativeApplication.exit() method?

  3. That works too, but then closing and close events are not dispatched to your windows. I like to use this code because it gives me the opportunity to hook into the closing event in any opened window if I ever need to (to prompt for save, for instance).Christian

  4. Gary says:

    Where would I put this code if I am using air with HTML/Javascript pages? I don’t have an mxml file?

  5. firos says:

    WindowApplication is the the top level application for Air.

    We can use FlexGlobals.topLevelApplication.close(); to close the entire air window from other MXML/Classes.