Flex Mobile:阻止退出按钮退出应用程序

时间:2013-11-18 15:51:42

标签: android actionscript-3 flex back-button flex-mobile

我正在为一些Android平板电脑开发Flex Mobile应用程序,直到现在我一直无法阻止Back Button离开应用程序。我仍然希望它是常规功能,只是我不希望它退出应用程序。我希望用户手动执行此操作。

关于如何实现这一目标的任何想法?也许通过捕捉一些事件?任何帮助将不胜感激。

此致

圣塞瓦斯蒂安

3 个答案:

答案 0 :(得分:1)

以下是我的处理方式:

<?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) {
            }
        }

即。不要调用任何基类方法,并在此处使用优先级1:

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

答案 1 :(得分:1)

解决此问题所需的唯一代码如下:

<s:ViewNavigatorApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                            xmlns:s="library://ns.adobe.com/flex/spark">

...

      <fx:Script>
        <![CDATA[

                   ...

            /**
             * Overrides the default backKeyUpHandler method so the event 
             * is stopped when there's only one View left on the navigator's stack. 
             */
            override protected function backKeyUpHandler(event:KeyboardEvent):void
            {               
                if (navigator.length == 1){                 
                    event.stopImmediatePropagation();                   
                    event.preventDefault();                 
                }else{
                    super.backKeyUpHandler(event)
                }
            }

        ]]>
    </fx:Script>

</s:ViewNavigatorApplication>

在你应该拥有的视图上:

<s:View xmlns:fx="http://ns.adobe.com/mxml/2009" 
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:componets="componets.*"       
        viewActivate="addKeyListeners(event)"
        viewDeactivate="removeKeyListeners(event)">

...
        <fx:Script>
        <![CDATA[
                          ...
            /**
             * Stops the Back key event.
             */
            private function keyDown(event:KeyboardEvent):void
            {               
                if(event.keyCode == Keyboard.BACK)
                {
                    event.stopImmediatePropagation();                   
                    event.preventDefault();                 
                }
            }
       ]]>
    </fx:Script>

</s:View>

感谢Alexander和Josh的帮助。

此致

圣塞瓦斯蒂安

答案 2 :(得分:0)

如果没有更多要播放的视图,ViewNavigator将退出应用。您可以使用ViewNavigator.length查看当前正在管理的视图数量。所以,从理论上讲,这样的事情应该有效:

stage.addEventListener( KeyboardEvent.KEY_UP, backButtonHandler );

private function backButtonHandler( e:KeyboardEvent ):void {
    if ( e.charCode == Keyboard.BACK && this.navigator.length == 1 ) {
        e.stopImmediatePropagation();
    }
}

现在,这是未经测试的。不过,我相信它应该有用。您可能需要检查长度是否等于0而不是1;我不确定当前的观点是否与我头顶的长度有关。但是,当没有更多的视图要弹出时,这应该可以防止后退按钮运行任何操作。