Working with Ionic v0.9.24 at the moment and I’m having a problem where the hardware back button on Android devices is closing the app instead of navigating back in the stack.
Is there something special I have to do to get this to use the history first then close the app?
tried a few things like listening for the backbutton event and calling e.preventDefault(); but that didn’t do anything.
I exactly have the same problem here.
Using AngularJS 1.3.0-beta, Ionic 1.0.0 Beta and Phonagap/Cordova 3.4.0 on Android 4.4 tablets and phones.
If you hit the android back button the app is closed immediately.
I tried to overwrite the native back button behaviour.
Listen for the backbutton event, prevent, stop propagate it and do the back handling on my own. I can catch the event, but after that the app is closed anyway.
I do not know if it is possible with only some code snippets.
But i can try to describe it a little bit more in detail.
I have an anuglarjs app built up with ionic and requirejs.
My angularjs routings are done with ui-router and $stateProvider.
In the app i navigate over $location.path(path) and back with $window.history.go(-steps).
After I bootstrap my angular app I hang in some event listeners - something like that:
document.addEventListener('deviceready', function () {
document.addEventListener('backbutton', function (event) {
event.preventDefault();
event.stopPropagation();
console.log('hey yaaahh');
}, false);
}, false);
If i tap on the android backbutton, i get my console log, but the app will be closed anyway.
$ionicPlatform.registerBackButtonAction(function () {
if (condition) {
navigator.app.exitApp();
} else {
handle back action!
}
}, 100);
$ionicPlatform.registerBackButtonAction allows you 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).
Thanks, registerBackButtonAction works, but it catches ALL back button actions,not only the controller I put the code in.
I want only one specific view to have a different behaviour (close the app on Back).
How can I keep default Back action on other views while having a special action on one view?
Edit (found):
// Disable BACK button on home
$ionicPlatform.registerBackButtonAction(function (event) {
if($state.current.name=="app.home"){
navigator.app.exitApp();
}
else {
navigator.app.backHistory();
}
}, 100);