1 Reply Latest reply on Oct 21, 2011 6:49 PM by William Spence

    CLOSING AN AIR APPLICATION

    William Spence Level 1

      I am writing an air application.  If a user clicks the close button up in the titlebar, I want to be able to create a pop-up that asks if they would like to save their project before exiting.  The problem is that the program just exits without displaying the pop-up.  I am listening for a "close" event and then calling a handler to issue the pop-up.  Using trace statements, I can confirm that it is making it to my handler but the application closes even though I don't issue a close() or exit() statement.  Here is the XML and the actionscript that I have(states and import statements are not included for brevity):

       

       

      <s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                                                           xmlns:s="library://ns.adobe.com/flex/spark" 
                                                           xmlns:mx="library://ns.adobe.com/flex/mx" width="1050" height="765"
                                                           applicationComplete="init()"  close="closeApplication(event)">
      
           <fx:Script>
                <![CDATA[               
      
                          protected function closeApplication(event:Event):void
                     {
                               if (this.currentState == "main" || this.currentState == "first" || this.currentState == "second"){
                                         trace("true");
                                         var savePopUp:SaveComponent = PopUpManager.createPopUp(videoGroup, SaveComponent, true) as SaveComponent;
                                         savePopUp.title = "Save";
                                         PopUpManager.centerPopUp(savePopUp);
                                }
                               else {
                                              trace("false");
                                          this.exit();
                               }
                     }
                ]]>
           </fx:Script>
      
      </s:WindowedApplication>
      
      

       

      If the application is in certain states, I want the user to recieve a popup used for saving a project before the program exits, but the program is not waiting for me to give the exit() or close() command.  Is there a way to override this, to inercept it, or something?  Or to listen for a different event?

        • 1. Re: CLOSING AN AIR APPLICATION
          William Spence Level 1

          I figured this one out on my own.  At the top of the closeApplication handler, you just have to add this line:

           

          event.preventDefault();

           

          This causes Flex to not close the program which is the default for clicking the "close" button.  Also, you should use NativeApplication.nativeApplication.exit() to close all the windows in the application instead of this.exit() as I did.