Programmatically close an AIR application

8.3k views Asked by At

I would like to know the correct way to close an AIR application programmatically.

In my Spark WindowedApplication I have:

this.addEventListener( Event.CLOSING, shutdownApp );

and of course an implementation of the shutdownApp method (which basically tidies up temporary files).

This works fine for the top-right close button of the window. However I also have functionality which needs to shutdown the application. Within the code I have called:

NativeApplication.nativeApplication.exit();

However this doesn't trigger the Event.CLOSING method, and so my temporary files are not cleared up. Should I not be calling nativeApplication.exit ? If so, what should I call instead? I'd rather not have to call my shutdownApp method before the NativeApplication.exit() as this doesn't feel quite so elegant.

Can anyone shed any light on the correct way of doing this?

Thanks,

Phil

3

There are 3 answers

1
JeffryHouser On BEST ANSWER

The documentation looks a bit ambiguous on this and I would have the same interpretation that you did. Did you try the close or exit methods on the WindowedApplication?

Something like this, with FlexGlobals and topLevelApplication:

(FlexGlobals.topLevelApplication as WindowedApplication).close();

or

(FlexGlobals.topLevelApplication as WindowedApplication).exit();
0
Dale Fraser On

I know this question has been answered and accepted, but thought I'd share, I use.

stage.nativeWindow.close();
0
Torsten Barthel On

Just give an answer here because I searched for a related question and was not able to find sth.

I wanted to do something similar and close an AIR application when the native close button of the document window (spark.components.Window) is pressed thought that the spark WindowedApplication container (the applications main window) is still active to hold and manage the native menu (at application startup it is also used to display a splash screen. It has to stay open since if it is closed the native menu won't show up or be accessible anymore so its visible property is just set to false).

My main problem was the window closing event. Registering it with ActionScript like

      this.addEventListener(Event.Closing, windowClosed);

does not work: No closing event was dispatched. The only way was to register an event handler directly in the s:Window element at start of the MXML file. I just throw in the closing attribute:

      closing="window1_closingHandler(event)"

The event was dispatched then and in window1_closingHandler-function I called

      NativeApplication.nativeApplication.exit();

That works for me and shuts down the whole application.