7 Replies Latest reply on Sep 13, 2009 11:27 AM by __antonio__

    AIR exit app and preventDefault

    paololazatin

      I'm experimenting with AIR after having learned AS3 recently. In the AIR test app that I am creating, I want to detect an application close in order to prompt the user for saving.  I looked at the documentation and some articles and this is what I have so far:

       

      NativeApplication.nativeApplication.addEventListener(Event.EXITING, closeApplication);

       

      public function closeApplication(e:Event):void
                  {           
                  trace("don't exit");
                  e.preventDefault();

                  trace(e.isDefaultPrevented);
                  }

       

      It doesn't seem to work though, I even tried Event.CLOSING, which does not even enter the function. The above code throws no errors, the string "don't exit", outputs true for the isDefaultPrevented, but still terminates. How do I intercept or overwrite the application exit in order to insert the prompt?

       

      By the way, I'm using pure AS3, as I'm not yet that familiar with Flex yet.

       

      Hope someone can guide me. Thanks!

        • 1. Re: AIR exit app and preventDefault
          __antonio__ Level 3

          Here is some code I copied out of a recent project:

           

          private function onClose():void
                         {
                              var closingEvent:Event;
                              closingEvent = new Event(Event.CLOSING, true, false);//(type, bubble?, cancelable?);
                              stage.nativeWindow.dispatchEvent(closingEvent);
                              if (!closingEvent.isDefaultPrevented())
                              {
                                   stage.nativeWindow.close();
                              } 
                              
                         }
          

           

          Basically, I'm stopping the window from closing if it already is. You might be able to rework it a little to bring up the save dialog.

           

           

          As a complete side note...

          Dont be afraid of Flex, and try not to compare the Flex Framework and the ActionScript 3 Language. The Flex Framework uses AS3. It also uses MXML, XML, CSS, JavaScript,.... AS3 is just a part of it.

          • 2. Re: AIR exit app and preventDefault
            paololazatin Level 1

            Hi Antonio,

             

            thanks for the code, however, but it still didn't work for me. As for my code, I made a few changes and stripped the application to trace what's going wrong.

            package
                 {
                 import flash.display.MovieClip;
                 import flash.events.Event;
                 import flash.events.MouseEvent;
                 import flash.desktop.NativeApplication;
                      
                 public class testAIR extends MovieClip
                      {            
                      public function testAIR():void
                           {
                           NativeApplication.nativeApplication.addEventListener(Event.EXITING, closeApplication); 
                           }
            
                      public function closeApplication(e:Event):void
                           {               
                           trace("don't exit");
                           e.preventDefault();
                           if (!e.isDefaultPrevented())
                                stage.nativeWindow.close();
                           else 
                                trace("said don't exit");
                           } 
                      }
                 
                 }
            

            .

            The e.isDefaultPrevented() outputs true, yet it still exits. I thought that it had something to do with running the AIR app in the Flash IDE (Maybe the exit intercept was disabled as a security measure) and so I installed the AIR app. Closing the program generated by the above code still exited the app, but in the taskManager, the process remains active until I terminate it there. And slightly out of topic,  I noticed that before I stripped this down, the AIR movie worked fine when testing in the Flash IDE, but not when I actually install the application (the graphics flicker, like how it is when you compile a project that throws an error, leaving the compile incomplete). Maybe it has something to do with my app exit code (since it didn't happen until I placed it). Might pinpoint what I'm doing wrong.

             

            *Better yet, perhaps someone can post a working AS3 code of a simple program that intercepts AIR app closing.

             

            As for your sidenote, I'm slowly trying to learn Flex (watching Lee Brimelow's videos and surfing around for articles), although I was thinking of mastering AS3 first before jumping to Flex (especially since I read somewhere that MXML is converted to AS3).

            • 3. Re: AIR exit app and preventDefault
              __antonio__ Level 3

              ok, i get ya now. I toyed around with this for a little bit until it dawned on me like "duh". Check it:

               

              exit() is not the same as close(). BUT, when you close the last window in an application, it exits (unless you set autoExit to false). BUT since you prevented default (just before checking if its NOT prevented).... when you hit the "close" button in the top right, you're just closing the window, not actually exiting. So, you just need to cancel out the closing event as well, so the window doesn't close. You can do this pretty much the same way as exiting. Then when the process you are doing (saving for instance) is finished, you can step forward with exiting.

               

              check this link out for a little bit more detail:

              http://help.adobe.com/en_US/AIR/1.5/devappsflash/WS5b3ccc516d4fbf351e63e3d118676a5d46-8000 .html#WS5b3ccc516d4fbf351e63e3d118666ade46-7cd3

              1 person found this helpful
              • 5. Re: AIR exit app and preventDefault
                __antonio__ Level 3

                ok since i cant sleep:

                 

                package 
                
                {
                     import flash.display.MovieClip;
                     import flash.events.Event;
                     import flash.desktop.NativeApplication;
                     import flash.display.NativeWindow;
                     import flash.display.NativeWindowInitOptions;
                     import flash.display.NativeWindowSystemChrome;
                     import flash.display.NativeWindowType;
                     
                     public class stopExiting extends MovieClip
                     {
                          public function stopExiting()
                          {
                               stage.nativeWindow.addEventListener(Event.CLOSING, closeApplication, false, 0, true); 
                          }
                          
                          public function closeApplication(e:Event):void
                                {      
                              //trace("don't exit");
                              e.preventDefault();
                
                                    //here is where i can add my prompt
                
                              var windowOptions:NativeWindowInitOptions = new NativeWindowInitOptions();
                              windowOptions.systemChrome = NativeWindowSystemChrome.STANDARD;
                              windowOptions.type = NativeWindowType.NORMAL;
                
                               var newWin:NativeWindow = new NativeWindow(windowOptions);
                              newWin.width = 300;
                              newWin.height = 200;
                              newWin.title = "Are you sure?";
                              newWin.activate();
                              newWin.addEventListener(Event.CLOSING, exitAll);
                          }
                          
                          public function exitAll(e:Event):void
                          {
                                 NativeApplication.nativeApplication.exit();
                               
                           }
                               
                     }
                }
                
                

                 

                Obviously, newWin isnt a real prompt, but you can get the point with that. Also, the Closing event listener should probably should go into some kind of conditional.

                • 6. Re: AIR exit app and preventDefault
                  paololazatin Level 1

                  wow, the link you provided was spot on, I can't believe I missed that when searching around articles and docs. Also, thanks for providing a working basic code, I tried it out, and it works. Thanks a mil Antonio.

                  • 7. Re: AIR exit app and preventDefault
                    __antonio__ Level 3

                    no problem.