2 Replies Latest reply on Aug 24, 2011 8:36 AM by rtalton

    Flash Builder Mobile app and handling the hardware Back button

    rtalton Level 4

      I can't find a way to handle the back button when:

      1) using TabbedViewNavigatorApplication, and

      2) the current view is the first view in a tab's view stack.

       

      The app will only exit. I'm trying to intercept the back key press and instead of exiting, give user a choice of going to another tab.

      I've tried the keyDownHandler in the view, a backKeyPressed handler in the view, a keyDown handler in the TabbedViewNavigatorApplication, a keyUp handler in the TabbedViewNavigatorApplication and an override of the backKeyUpHandler() function of theTabbedViewNavigatorApplication.

        • 1. Re: Flash Builder Mobile app and handling the hardware Back button
          Shongrunden Adobe Employee

          Try having a backKeyPressed handler that calls event.preventDefault();     event.stopImmediatePropagation();

          • 2. Re: Flash Builder Mobile app and handling the hardware Back button
            rtalton Level 4

            Tried backKeyPressed with preventDefault & stopImmediatePropagation: it didn't work either.

            In case it would help anyone else, here's the way I got it to work in my TabbedViewNavigatorApplication:

            In each top-most View in each tab that you want to prevent the default back key press from exiting the app, add a handler to the ViewActivate event

             

            //add the keyboard listener:
            stage.addEventListener(KeyboardEvent.KEY_DOWN, _onKeyDown);

             

            In this View's Deactivate event listener, add this:

            //removes the _onKeyDown listener from this view when
            //the view is deactivated:
            stage.removeEventListener(KeyboardEvent.KEY_DOWN, _onKeyDown);

             

            Finally, the function which does the job of preventing accidentally exiting the app:

            private function _onKeyDown(event:KeyboardEvent):void {
                if (event.keyCode == Keyboard.BACK) {
                    event.preventDefault();
                    //Return user to the "Home" view, where they can exit if they wish.
                    this.parentApplication.tabbedNavigator.selectedIndex=0;
                } else if (event.keyCode == Keyboard.MENU) {
                    //unused
                    //event.preventDefault();
                } else if (event.keyCode == Keyboard.SEARCH) {
                    //unused
                    //event.preventDefault();
                }
            }

             

            This is based on code I got here:

            http://www.unitedmindset.com/jonbcampos/2010/09/17/air-for-android-home-menu-back-and-sear ch-buttons/

            but was based on the added to stage event, and i had issues with making that work as it was firing from every view and was difficult to sort out a condition to actually supress the event. So the way I've done it, I know which view fired the event as it can only originate from a single view while it is active.

             

            Hope this helps someone researching the same issue in the future.