Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 关闭背面的cordova应用程序按钮_Android_Angularjs_Cordova - Fatal编程技术网

Android 关闭背面的cordova应用程序按钮

Android 关闭背面的cordova应用程序按钮,android,angularjs,cordova,Android,Angularjs,Cordova,我使用cordova和angular.js为我的移动应用程序提供以下逻辑 在index.html中包含的mobile.js中处理我的逻辑,比如在sdcard上保存文件,然后使用 window.location.href="main.html"; 将使用mobile.js放入sdcard的文件 我面临的问题是,当我在main.html的主页上,用户按下后退按钮时,它会返回index.html文件,然后在处理后返回main.html,而不是关闭应用程序 我尝试将history.length对象与“

我使用cordova和angular.js为我的移动应用程序提供以下逻辑

在index.html中包含的mobile.js中处理我的逻辑,比如在sdcard上保存文件,然后使用

window.location.href="main.html";
将使用mobile.js放入sdcard的文件

我面临的问题是,当我在main.html的主页上,用户按下后退按钮时,它会返回index.html文件,然后在处理后返回main.html,而不是关闭应用程序

我尝试将history.length对象与“backbutton”eventListener一起使用

        document.addEventListener('deviceready',function(){
        document.addEventListener('backbutton',function(e){
            console.log("history is "+history.length);
            if(history.length==1){
                e.preventDefault();
                navigator.app.exitApp();
            }
            else{
                navigator.app.backHistory();
            }
        },false);
    },false);
但返回时它不会减少长度,只会增加长度,因此应用程序返回index.html。(history.length始终大于1)

我已经找到了可用的解决方案,如

document.addEventListener("backbutton", function(e){
if($.mobile.activePage.is('#homepage')){
    /* 
     Event preventDefault/stopPropagation not required as adding backbutton
      listener itself override the default behaviour. Refer below PhoneGap link.
    */
    //e.preventDefault();
    navigator.app.exitApp();
}
else {
    navigator.app.backHistory()
} 
}, false);
但是使用它的问题是,如果用户

second-page->homepage->third-page->homepage

应用程序将退出,但应转到第三页。

您可以使用jQuery mobile pageload事件保留自己的历史记录列表,返回时其长度会减少。类似这样的情况(未经我的测试,因此可能不完全正确):

var pageHistory = [];

$(document).on("deviceready", onDeviceReady);


function onDeviceReady(){
    $(document).on("pagecontainerload", onPageLoad);
    $(document).on("backbutton", onBackButton);
}

function onBackButton(e){
    e.preventDefault();
    pageHistory.pop();
    if(pageHistory.length==0){
        navigator.app.exitApp();
    }
    else{
        navigator.app.backHistory();
    }
}

function onPageLoad(e, ui){
    var pageId = ui.toPage.attr('id');
    if(pageId !== pageHistory[pageHistory.length]){
        pageHistory.push(pageId);
    }    
}