Search code examples
androidactionscript-3apache-flexback-buttonflex-mobile

Flex Mobile: preventing back button from exiting app


I'm developing a Flex Mobile application for some android tablet and until now I've been unable to prevent the Back Button from leaving the application. I still want it's regular functionality, it's just that I don't want it to exit the app. I want the user to do this manually.

Any ideas on how to achieve this? Maybe by catching some event? Any help will be greatly appreciated.

Regards,

Sebastián


Solution

  • Here is how I handle it:

    <?xml version="1.0" encoding="utf-8"?>
    <s:View xmlns:fx="http://ns.adobe.com/mxml/2009" 
            xmlns:s="library://ns.adobe.com/flex/spark"
            initialize="init()"
            viewActivate="activate(event)"
            viewDeactivate="deactivate(event)">
    
    <fx:Declarations>
        <fx:Component className="ConfirmExit">
            <s:Callout 
                horizontalPosition="middle"
                verticalPosition="middle">
                <s:VGroup width="100%">
    
                    <s:Label text="Really quit?" />
    
                    <s:HGroup width="100%">
    
                        <s:Button label="Yes"
                                  click="close(true)" />
                        <s:Button label="No"
                                  click="close(false)" />
                    </s:HGroup>
                </s:VGroup>
            </s:Callout>        
        </fx:Component>
    </fx:Declarations>
    
    <fx:Script>
        <![CDATA[
            import spark.events.ViewNavigatorEvent;
    
            private function activate(event:ViewNavigatorEvent):void {
                // add exit confirmation dialog - Samsung Apps requirement
                if (Capabilities.version.indexOf('AND') >= 0 &&
                    Capabilities.manufacturer.search(/Samsung/i) >= 0) {
                    _confirmExit.addEventListener(PopUpEvent.CLOSE, handleLeaveCallback, false, 0, true);
                    stage.addEventListener(KeyboardEvent.KEY_UP, handleKeyUp, false, 1, true);
                } 
            }
    
            private function deactivate(event:ViewNavigatorEvent):void {
                _confirmExit.removeEventListener(PopUpEvent.CLOSE, handleLeaveCallback);
                stage.removeEventListener(KeyboardEvent.KEY_UP, handleKeyUp);
            }
    
            private function handleKeyUp(event:KeyboardEvent):void {
                if (event.keyCode == Keyboard.BACK /* && navigator.length == 1 */) {
                    _confirmExit.open(this, true);
                    // event.preventDefault();
                }
            }
    
            private function handleLeaveCallback(event:PopUpEvent):void {
                if (!event.commit)
                    return;
    
                try {
                    NativeApplication.nativeApplication.exit();
                } catch (e:Error) {
                }
            }
    

    I.e. don't call any base class methods and use the priority of 1 here:

    stage.addEventListener(KeyboardEvent.KEY_UP, handleKeyUp, false, 1, true);