7 Replies Latest reply: Feb 11, 2012 9:17 AM by GrindheadGamess RSS

    Air for Android - how to handle back button?

    LisaMStump Community Member

      I have two questions:

      1.  Where is the Adobe Air for Android (or Flash for Android) or Adobe sanctioned mobile development forum?

      2. I am developing a Flash/AS3 app for Android.  How can I handle the back button to go back within my own application when appropriate?

        • 1. Re: Air for Android - how to handle back button?
          ShaileshMak Community Member

          1) You can register for Pre Release programm (https://prerelease.adobe.com/) and get the AIR SDK for the Android.

          2) The Back, Menu, Search button works with Keyboard Event. You can add the Key Event to the stage and Check the keyCode with the Keyboard class constance like, Keyboard.BACK, Keyboard.MENU, Keyboard.SEARCH.

           

          I hope this answers your question.

          • 2. Re: Air for Android - how to handle back button?
            phillipkerman ACP

            My code is working insofar as the menu button gets captured as well as the back button.. but I can only see my back button is function right before it goes to the previous application running on my nexus 1.  I even added the uses permission FORCE_BACK.  Please advise if this is possible or reliable on a range of devices. Thanks!

            • 3. Re: Air for Android - how to handle back button?
              LisaMStump Community Member

              Hi Phillip,

               

              You need to call preventDefault() and stopImmediatePropagation() on the KeyboardEvent.

               

               

              Here is my code:

               

                      function onKeyPress(ev:KeyboardEvent):void
                      {
                          switch(ev.keyCode)
                          {
                              case Keyboard.BACK: // user hit the back button on Android device
                              // case 94: // was hard-coded for older build of SDK supporting eclair
                              { 
                                  if(null != _currentScreen)
                                  {
                                      // TO DO - make this smarter?
                                      if(_currentScreen.goBack())
                                      {
                                          // don't let the OS kill focus to our app
                                          ev.preventDefault();
                                          ev.stopImmediatePropagation();
                                      }
                                  }
                                  break;
                              }

                        }

               

              - Lisa

              • 4. Re: Air for Android - how to handle back button?
                phillipkerman ACP

                Thanks Lisa!  It works great.

                • 5. Re: Air for Android - how to handle back button?
                  JonKaye Community Member

                  I am not getting the default behavior to be overriden -- I am trying to prevent the back button from popping the view by default (I have a Samsung Charge, FB 4.5, Windows).  It gets into the key handler, but the instructions appear to be ignored:

                   

                  protected function onKeyDown (event:KeyboardEvent):void {

                    event.preventDefault();

                    event.stopImmediatePropagation();

                  }

                   

                  and I registered the listener with priority 0 (even tried true and false for useCapture, to see if for some reason it was the listener order), but it's still doing the default behavior of popping the view.

                   

                  Any thoughts about why this is not working?

                  • 6. Re: Air for Android - how to handle back button?
                    JonKaye Community Member

                    I figured it out, though I don't know why the key handler was not working.

                     

                    The View class has an event "backKeyPressed", so I can put the preventDefault() and stop propagation there.  I don't need all the key listener stuff and such.

                    • 7. Re: Air for Android - how to handle back button?
                      GrindheadGamess Community Member

                      I wrote a small tutorial on this article here: http://www.grindheadgames.com/handling-button-air-android I hope someone finds it useful.