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);
to override the back button but it still looks for previous views to go !
it does get called but it doesnt override the Back Button
What could be wrong ?
Can anyone help ?
If you end up overwriting this a lot, it get’s really clunky. Here’s a simple angular service I wrote that allows you to easily attach back button behavior to specific app states:
I was searching for a similar solution and seems like I finally found it. I have slightly out of context question. Where can I find documentation for “navigator.app” is it core feature of ionic? cordova? or some plugin?
There is no documentation on navigator.app for cordova, and that is on purpose.
It contains APIs and behaviour that are specific to certain platforms, and can’t/won’t be normalized accross other platforms (such as navigator.app.exitApp()).
Although I haven’t seen word on it from the core developers, I wouldn’t even count on it to be there everytime: it may not exist on the next version of cordova at all.