如何在一个页面上禁用Android Back按钮并在每个其他页面上更改为退出按钮

Obs*_*wer 14 android back-button-control jquery-mobile cordova

我正在使用与我的Drupal站点交互的Phonegap开发一个Android应用程序.我已经重新分配Android"后退"按钮以提示用户从Drupal服务器注销,但是我只是想在登录页面上禁用它(原因很明显).我可以让它工作,但只有在用户注销然后在登录页面上,按钮仍然被重新分配为注销按钮.这是我到目前为止的代码:

   <head>
         <script>
        /* Wait until device is ready to re-assign Back button */
        document.addEventListener("deviceready", onDeviceReady, false);
        function onDeviceReady() {
            document.addEventListener("backbutton", onBackKeyPress, false);
        }
        function onBackKeyPress() {
            /* If the current page is the login page, disable the button completely (aka do nothing) */
            if ($.mobile.activePage.attr('id') == 'login_page') {
            }

            /* Else, execute log off code */
            else {
                if (confirm("Are you sure you want to logout?")) {
                    /* Here is where my AJAX code for logging off goes */
                }
                else {
                    return false;
                }
            }
        }
        </script>
</head>
Run Code Online (Sandbox Code Playgroud)

问题是没有重新分配后退按钮.当用户注销并最终返回登录页面时,我无法找到重新运行上述代码的方法.

如果有人愿意为此提供解决方案,我将非常感激!

OSP*_*OSP 23

deviceready很重要.如果不使用,有时你可以阻止Back按钮,有时候不会.通常在调试中它起作用,在生产中没有.

document.addEventListener("deviceready", onDeviceReady, false);
    function onDeviceReady() {
        document.addEventListener("backbutton", function (e) {
            e.preventDefault();
        }, false );
}
Run Code Online (Sandbox Code Playgroud)

编辑2013-11-03常见的错误是在桌面上执行开发并且排除了cordova脚本.然后忘记包含移动版的cordova脚本.


A. *_*ães 5

试试这个:

document.addEventListener("backbutton", function(e){
    if($.mobile.activePage.is('#login_page')){
        e.preventDefault();
    }
    else {
        if (confirm("Are you sure you want to logout?")) {
            /* Here is where my AJAX code for logging off goes */
        }
        else {
            return false;
        }
    }
}, false);
Run Code Online (Sandbox Code Playgroud)