본문 바로가기

공부방/Flex

[FlexMobile] Preventing Back Button Default When Navigating Views in Flex Hero Mobile Application

Ignore this post! See how to do it better here. I am leaving this post up for reference.

Preventing Back Button Default When Navigating Views in Flex Hero Mobile Application

Items Covered: Flex Hero Views, Hardware Back Button on Android Devices
Flex/AIR Version: 4.5 (Hero), AIR Android 2.5
Difficulty Level: Beginner
Prerequisite Knowledge: How Views work in Flex 4.5 Mobile Application
Sample Application: ViewNavFix.apk (I would show this in browser but it is a mobile app, so you’ll need to download and either run it on a device or in Emulator)
Sample Application Files: ViewNavFix.fxp (You will need Flash Builder Burrito)

Desired Interaction:

You want to prevent the hardware BACK button from firing a popView() event when in a View. Maybe this is to bring up a confirmation dialog or perform some other action. Then, go back to the previous view when another button is pressed.

Problem:

Simply using preventDefault() when listening to the Keyboard.BACK down (or up) event does not actually prevent the popView() event. It seems as though there is a bug where NativeApplication still hears when the Keyboard (hardware buttons) are pressed, so your application will always preform the default action when the hardware buttons are pressed. This happens whenever there is not anything on stage in focus.  In addition, the event listeners can not be fully removed from within a view.

Solution:

You must prevent default in the main Application file, as well as within any View. In essence you MUST handle all the hardware keys’ listeners yourself, in every state of the application. You must also have something on stage in focus to prevent the default action of the hardware buttons.

ViewNavFix.mxml

First, I had to preventDefault on the BACK button in the main Application file. This means I will then need to handle the back button interactions. See the code:
 



    
        
    

    
        
    



ViewNavFixHome.mxml

Since I have to handle the Back button my own now in the Home View, I have to preventDefault() and perform my desired action. I am listening for the Back button from NativeApplication to exit(), and pushing to a second View with navigator.pushView();. And since I am doing this I have to add the event listeners on viewActivate(). For some reason the listeners bubble to the previous next view so I have to remove them on viewDeactivate().