Trouble getting ng-device-back-button directive to work for OnsenUI v2

107 Views Asked by At

I'm attempting to stop a user from abandoning a game by accidentally clicking the device back button on Android devices. I'm using Cordova 6.3.1 and the Onsen UI v2 framework.

By using an Onsen page's ng-device-back-button attribute, I have been able to completely disable the back button, but I would prefer to ask a user if they wanted to leave with a confirm. At this point, I would be happy if I could just write to the console that the user has clicked the device back button, but it seems like nothing is happening other than the ng-device-back-button attribute preventing the default.

<ons-page ng-device-back-button="onBackKeyDown">
    ...
</ons-page>

$scope.onBackKeyDown = function(e) {
    console.log("device back button pressed");
}

With the above code, I'm able to prevent Onsen from the standard popPage() reaction, but nothing is being written to the console.

Any thoughts on what I'm doing incorrectly? Thanks in advance.

2

There are 2 best solutions below

0
On BEST ANSWER

Ultimately, I was able to get this to work by getting the latest version of Onsen UI. Who would have thought using an outdated version could cause problems? ;)

1
On

try

ng-device-back-button="onBackKeyDown()"

instead of

ng-device-back-button="onBackKeyDown"