1

I have an universal app with Cordova and PhoneJS and build it with Phonegap for iOS, Android and Windows Phone. The Windows Phone style removes the back button on views to navigate back. When I press the hardware back button, the app exits.

Thats why I want to override the back button functionality. I found a lot of documentation which states that you need to register on the 'backbutton' event on 'deviceready' after Cordova loads.

The 'on load' and 'deviceready' events are invoked successfully. The problem is that the back button event is not invoked and the app still exits.

Versions:

npm list -g cordova
...\AppData\Roaming\npm
└─┬ phonegap@5.3.7
  └── cordova@5.4.0

Device:

  • Microsoft Lumia 640 LTE
  • Windows Phone 8.1 Update 2

Code:

// Is invoked
function onLoad() {
    document.addEventListener("deviceready", onDeviceReady, false);
}

// Is invoked
function onDeviceReady() {
    document.addEventListener("backbutton", onBackButton, false);
}

// Is not invoked
function onBackButton(){
  debugger;
}
<body onload="onLoad()">
</body>
Justin
  • 2,960
  • 2
  • 34
  • 48

1 Answers1

0

I found out that the included PhoneJS uses WinJS. With that I could set the 'onbackclick' action.

function onLoad() {
    document.addEventListener("deviceready", onDeviceReady, false);
}

function onDeviceReady() {
    // Check if WinJS api is available
    if(WinJS){
      WinJS.Application.onbackclick = function (e) {
        MyApp.app.navigationManager.back();

        // Return true otherwise it will close app.
        return true;
      }
    }
}
<body onload="onLoad()">
</body>
Justin
  • 2,960
  • 2
  • 34
  • 48