You want to prompt the user before quitting the application. "Are you sure you want to quit?"
You can catch and stop an Event.CLOSING event on a window. If it's the main application window, simply prevent the event from propagating.
I know I'm as guilty as anyone on this -- Trying to quit Flash, Photoshop or some other program in the middle of editing an unsaved document. Thank god for that prompt. You know... the one that tells you that you have unsaved changes and you should probably consider saving them before obliterating them? AIR supports this sort of thing, though it's a bit obscure how. When a user exits a window, it fires an event right before it closes (aptly named Event.CLOSING). You can leverage the stopPropagation(); method to prevent the window from closing.
I wrote this class, called NativeWindowManager, which I use in conjunction with NativeWindow. It dispatches "promptQuit", if the Boolean isQuittingBlocked is set to true. When I receive the event, I display a prompt the user and if they click "Yes" to closing the window anyway, I call the close(); method on my NativeWindowManager and it will close the window for me. I also built in a way to close every window related to this application at the same time, simply by passing "true" as the second parameter to my NativeWindowManager. This effectively allows me to treat one window as the "main" window -- when it closes, the program exits.
package com.bigspaceship.core.air
{
// flash
import flash.display.NativeWindow;
import flash.desktop.NativeApplication;
import flash.events.Event;
import flash.events.EventDispatcher;
public class NativeWindowManager extends EventDispatcher
{
private var _window :NativeWindow;
private var _isMainWindow :Boolean;
public var isQuitBlocked :Boolean;
public static const PROMPT :String = "windowQuitPrompt";
public function NativeWindowManager($window:NativeWindow,$isMainWindow:Boolean = false):void
{
_window = $window;
_isMainWindow = $isMainWindow;
_window.addEventListener(Event.CLOSING,_windowOnClose,false,0,true);
};
public function get window():NativeWindow { return _window; };
private function _mainWindowOnClose($evt:Event):void
{
$evt.preventDefault();
if(isQuitBlocked) dispatchEvent(new Event(PROMPT));
else close();
};
public function close():void
{
_window.removeEventListener(Event.CLOSING,_windowOnClose);
if(_isMainWindow) NativeApplication.nativeApplication.exit();
else _window.close();
_window = null;
};
};
};
+