I'm trying to disable the back button on my Cordova app. I'm using AngularJS + Ionic Framework. I found topics about this and tried the code bellow, but it has absolutely no effect. Any idea? Thanks!
index.html
<head>
<script>
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
document.addEventListener("backbutton", function (e) {
e.preventDefault();
console.log("hello");
}, false );
}
</script>
</head>
Note that when I push back button, I have "hello" displayed in my console.
Finally found the answer on this Ionic Forum thread:
$ionicPlatform.registerBackButtonAction
allows to completly overwrite back button behavior. First param is a callback function and the secondone a priority (only the callback with the highest priority gets executed).this will prevent back button functionality.
Its simple trick prevent go back to single page:
To expand upon David D's answer I have included the go back implementation.
Put this in your applications
.run
function:This will not work in controllers, it is application wide.
To disable hardware back button in Ionic application for controller (or controller of component), you can make the following workaround, but first it is actually not for controller itself, but it combination between controllers and state, in your controller, add your normal code:
But in your
$stateProvider
adddisableHardwareBackButton
like the following:Inside your module('app').run function:
In this way you get around the issue with "sub section" or "inside controller"
To prevent App from device back button functionality use,
If you want to prevent the particular page use,