Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"backbutton" event won't fire

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>
like image 991
Jonathan Whitney Avatar asked Aug 08 '12 14:08

Jonathan Whitney


1 Answers

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.

like image 125
opznhaarlems Avatar answered Sep 28 '22 13:09

opznhaarlems