Question

I am attempting to build a phonegap app for Windows Phone 7. I am trying to follow the documentation for the "backbutton" event (http://docs.phonegap.com/en/2.0.0/cordova_events_events.md.html#backbutton), but I can't seem to get it to work.

The "deviceready" event fires, but the "backbutton" event does not. When compiling and running in Visual Studio Windows Phone emulator the onDeviceReady function is called and "Device ready" is logged, but when the emulator back button is pressed the application exits and nothing is logged in the console. When the back button is pressed the OnBackKeyDown function should run.

copy of the code from the offical doc:

<html>
    <head>     
        <script type="text/javascript" charset="utf-8" src="cordova-2.0.0.js"></script>
        <script type="text/javascript">
            function onLoad() {
                document.addEventListener("deviceready", onDeviceReady, false);
            }
            function onDeviceReady() {
                console.log("Device ready");
                document.addEventListener("backbutton", onBackKeyDown, false);
            }
            function onBackKeyDown() {
                console.log("Back button pressed");
            }
        </script>
    </head>
    <body onload="onLoad()">
        <div></div>
    </body>
</html>
Was it helpful?

Solution

The fix has been committed here: https://github.com/purplecabbage/incubator-cordova-wp7/commit/d04b87abb3c3822ef25438e1353a1d7d2e0d6628

You will need to wait for 2.1.0 to be released early next week, or build your app from source-code, in the meantime.

OTHER TIPS

I managed to fix this by copying some parts of cordova-1.8.1.js to cordova-2.0.0.js.

In 1.8.1, search for: var NamedArgs and copy the whole object to 2.0.0.

In 2.0.0, search for: var command = service + "/" + action + "/" + callbackId + "/" + JSON.stringify(args); and replace it with:

if ( action == 'overridebackbutton' ) {
    if ( NamedArgs[service] && NamedArgs[service][action]) {
        var argNames = NamedArgs[service][action];
        var newArgs = {};
        var len = Math.min(args.length,argNames.length);

        for(var n = 0; n < len; n++) {
            newArgs[argNames[n]] = args[n];
        }

        args = newArgs;
    }
    else if(args && args.length && args.length == 1) {
        args = args[0];
    }
}
var command = service + "/" + action + "/" + callbackId + "/" + JSON.stringify(args);

This might not be a pretty solution, but it works for me.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top