1 Reply Latest reply on Feb 27, 2009 2:35 PM by Joe ... Ward

    run a function before exiting

    MixMastaJames
      I'm working with an AIR app that I want to save a file before the window closes. I have the code run on the "Exit" button, but if they just close the chrome window with the "X" it won't run...

      so I tried this:

      this.addEventListener(Event.EXITING, doSaveXML);

      private function doSaveXML(event:Event):void {
      if(lessonXML != null) {
      saveXML();
      }
      }

      and thought that it would run that code and save the xml before the window closes

      it closes with this.exit();

      how can I get it to run that function BEFORE it closes to save the xml file?
        • 1. Re: run a function before exiting
          Joe ... Ward Level 4
          When YOU call the exit() function, no exiting event is dispatched. If you need this behavior, the proper thing to do is to dispatch an exiting event yourself as shown here:

          private function exitApp(event:Event):void
          {
          var exitingEvent:Event = new Event( Event.EXITING );
          NativeApplication.nativeApplication.dispatchEvent( exitingEvent );
          if( !exitingEvent.isDefaultPrevented() )
          {
          NativeApplication.nativeApplication.exit();
          }
          }

          Also, you should careful about asynchronous operations in saveXML() as they might not return before the application exits. In that case, you would have to call event.preventDefault() in your exiting event handler and then call exit() when finished.