Implementing Back Button functionality for wp8 cordova app

I tried to implement the functionality of the back button in my wp8 cordova application, which, when I click the back button on the device, should go to the previous page of the application.

What I've done

function onLoad() {
    document.addEventListener("deviceready", init, false);
    document.addEventListener("resume", onResume, false);
    document.addEventListener("backbutton", onBackKeyDown, false);
}

function init() {
  //some code
}

function onResume() {
  //some code   
}

function onBackKeyDown() {
   window.history.back();
   return false; 
}

I also tried replacing "window.history.back ();" with "navigator.app.backHistory ();" which also doesn't seem to work

Then I tried to put the code in a catch catch block

try
{
navigator.app.backHistory();
//window.history.back(); 
}
catch (e)
{
console.log("exception: " + e.message); 
}

which also seems to fail. Be that as it may, the application seems to exit the application rather than moving backward, and the funny thing is, when I try to do this on the IE console, it seems to work fine.

Help with the guys

Thanks in advance

+4
1

, , wp8. WinJS Framework :

onDeviceReady :

if (device.platform == "windows") {
    // Get the back button working in WP8.1
    WinJS.Application.onbackclick = function () {
        onBackKeyDown();
        return true; // This line is important, without it the app closes.
    }
}
else {
    document.addEventListener("backbutton", onBackKeyDown, false);
}

onBackKeyDown :

function onBackKeyDown() {
    // Back key pressed, do something here
}

BackButton-Event :

<!DOCTYPE html>
<html>
  <head>
    <title>Back Button Example</title>

    <script type="text/javascript" charset="utf-8" src="cordova.js"></script>
    <script type="text/javascript" charset="utf-8">

    // Wait for device API libraries to load
    //
    function onLoad() {
        document.addEventListener("deviceready", onDeviceReady, false);
    }

    // device APIs are available
    //
    function onDeviceReady() {
        // Register the event listener
        document.addEventListener("backbutton", onBackKeyDown, false);
    }

    // Handle the back button
    //
    function onBackKeyDown() {
    }

    </script>
  </head>
  <body onload="onLoad()">
  </body>
</html>

, BackButton DeviceReady!

+6

All Articles