Guillermo de la Iglesia

Here I will explain how to create an Alert window when we press the exit/close button in an AIR application, so that we have to validate this exiting avoiding that it closes up directly. This way we can control that the user closes the application without having accomplished a necessary operation, for example, without saving the changes made in an application that edits documents.

The first thing we have to do is place/write the function that must be done when it detects the event of closing up the application (that is to say, when the user closes up the application).
Now we will write the function. The important issue here is how to avoid the closing of the application, for the user has really clicked the closing button. We will achieve this by the method preventDefault() of the Event class.

With this method we will avoid that the event executes the functions by default. Instead of this it will open a window of Alert.

With yesLabel we are saying that it has to appear the text "If" in the validating button instead of "Yes". And with the 2 at the end of the parameters we are saying that the selected button by default must be the second one, that is, "No".
We are also indicating that when clicking in any button it shall execute the function "cerrar" that is the following:

We are indicating now that if the button selected is "If" the application must close. In the case of selecting "No" nothing executes, and so the application remains open.

Second example

The former code would execute whenever we press the application close icon. The interesting this is to be able to control when we will want to display this warning.
For example, in the case when we have a text edit application. If we press the application close button without having saved the changes we will display the Alert, but in the case we have previously saved these changes we will directly close the application.

We will first create a booleana variable that will control if the application closes up automatically or requieres to display the warning.

private var cierre:Boolean = true;

We will place a conditional in the function that executes when closing the application
In the function that is executed when closing the application we will place a conditional that detects the state of this variable.

Logically we will have to control the function "cierre" with the rest of the functions of the application. In other words, all functions that execute any change that must be saved must place/write that variable "false", and the function that executes the saving the data will have to write the variable "true".
The closing function will be as follows:

So that if we click "If" we would execute the function that saves the data and closes then the application, and if we clicked "No" we would directly exit the application and if we clicked "Cancel" then nothing would be executed and we wouldn't exit the application.

Excellent post. However you missed the most important point: how in God's name to detect de app is closing (not what to do when it does). The event listener should be attached to... what? What's the event name?