简体   繁体   中英

Trouble with backbutton in Windows Phone on Cordova/Phonegap app

I'm experience a trouble that i can't solve.

I'm trying to manipulate the backbutton of a WindowsPhone but it doesn't seem to work

I've tried to re-use an event (that actually works on Android app), that its more less like this

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();
        }
    }

Since its doesnt seem to work on windows phone and after some research i've installed via nuget WinJS and tried to call the function like this

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

I'm calling this event inside the deviceready event, so I'm out of solutions.

It seems that it doesnt detect the event or even the function. So I'm wondering what I'm doing wrong.

Any sugestion?

I hope you have fixed your issue, If not you can try below method.

You need to include the backbutton() function in index.js file of the Cordova project.

You can implement something similar to below,

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
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM