Cordova / Phonegap应用程序

时间:2015-07-10 12:20:50

标签: jquery cordova windows-phone-8.1

我遇到了一个我无法解决的麻烦。

我正在尝试操纵WindowsPhone的后退按钮,但它似乎无法正常工作

我试图重新使用一个事件(实际上适用于Android应用),它更像是这样

document.addEventListener("backbutton", onBackKeyDown, false);

function onBackKeyDown(e) {
        var location = window.location.href;
        console.log(location);
        var partial_location = location.split("#");
        if (partial_location[1] == "menu/" || partial_location[1] == "login/") {
            e.preventDefault();
            navigator.app.exitApp();
        } else {
            navigator.app.backHistory();
        }
    }

因为它似乎不适用于Windows手机,经过一些研究我已经通过nuget WinJS安装并试图调用这样的功能

if (device.platform == "windows") {
        WinJS.Application.onbackclick = function(evt){
            onBackKeyDown(evt);
            return true;
        }
    } else {
        document.addEventListener("backbutton", onBackKeyDown, false);
    }

我在deviceready事件中调用此事件,因此我没有解决方案。

它似乎没有检测到事件甚至是功能。所以我想知道我做错了什么。

任何消化?

1 个答案:

答案 0 :(得分:0)

我希望您已经解决了问题,如果没有,您可以尝试以下方法。

您需要在Cordova项目的index.js文件中包含backbutton()函数。

你可以实现类似于下面的内容,

var app = {
    // Application Constructor
    initialize: function() {
        this.bindEvents();
    },
    // Bind event listeners for 'deviceready' and 'backbutton' 
    bindEvents: function() {
        document.addEventListener('deviceready', this.onDeviceReady, false);
        // Here define your back button listener
        document.addEventListener('backbutton', this.handleBackButton, false);
        // If you need to support native back button you can do like this
        // document.addEventListener('backbutton', this.handleBackButton, true);
    },

    // deviceready Event Handler
    onDeviceReady: function () {
        //OnDeviceReady function implementation    
    },

    // backbutton Event handler
    // Here define your logics
    handleBackButton: function () {
        // Check if you are in home page
        // You need to check according to your application. For example if the home page (data page) is index-in, show alert message
        if ($('.page-on-center').data('page') == "index-in") {
            myApp.confirm('Are you sure you want to exit ?', 'Exit',
              function () {
                  // If Click EXIT or Confirm
                  // This is not work for me
                  //navigator.navigation.exitApp();
                  // This works fine for me
                  navigator.app.exitApp();
              },
              function () {
                  // If click cancel do nothing
              }
            );
        } else {
            // Go to previous page
            mainView.router.back();
        }
    },
// do something other than the default, like undo an operation,
// or step backwards through a multi-step operation
};