The alert is shown but ionic still changes the view showing that the functionality of the button is not disabled. What’s wrong with my code or my understanding?
I also tried the following code:
document.addEventListener("backbutton", onBackKeyDown, false);
function onBackKeyDown(e) {
e.preventDefault();
alert('backbutton disabled');
}
Codes below is an example to disable Android H/W BACK button for two specific states - “app.state1” and “app.state2”. If you want to disable more states, you can add them to the if condition.
app.run(function($ionicPlatform, $state){
$ionicPlatform.registerBackButtonAction(function (event) {
if ( ($state.$current.name=="app.state1") ||
($state.$current.name=="app.state2")
){
// H/W BACK button is disabled for these states (these views)
// Do not go to the previous state (or view) for these states.
// Do nothing here to disable H/W back button.
} else {
// For all other states, the H/W BACK button is enabled
navigator.app.backHistory();
}
}, 100);
})
I have a question with your code.
If you bind an event to the Hardware back button on priority 100, if the app is in the first state and you press back button, what happens? The app is closed or nothing happens? ( because there are no backhistory)
bengtler is right.
On my original response, I made a mistake by not including the following line:
"$ionicPlatform.registerBackButtonAction(function (event) { ".
I corrected my post above and included the above line.
So my code now looks like the following:
app.run(function($ionicPlatform, $state){
$ionicPlatform.registerBackButtonAction(function (event) {
if ( ($state.$current.name=="app.state1") ||
($state.$current.name=="app.state2")
){
// H/W BACK button is disabled for these states (these views)
// Do not go to the previous state (or view) for these states.
// Do nothing here to disable H/W back button.
} else {
// For all other states, the H/W BACK button is enabled
navigator.app.backHistory();
}
}, 100);
})
Thanks for your answer bengtler, I tried both registerBackButtonAction and onHardwareBackButton but they had the same behavior that cauboy explained in his first post.
But reading again the documentation, I have been able to fix my problem by adding a priority to the registerBackButtonAction. So, thanks
Hi! I don’t know if this is offtopic but i have a question about this issue.
I was using beta13 and I realized that onHarwareBackButton and registerBackButtonAction were not working on my app. Unfortunately i cant update my app to the latest version, but i tried creating a new ionic project and in the latest version i was able to disable the back button behaviour.
My question is the following: Did you guys resolve this issue on a Javascript level? or, did you modify native android/iOs code to access that button/override it’s behaviour?
I was wandering if i could find the change in your source code and apply it to my beta13 app.
Thank you! And sorry if this is offtopic, let me know and I’ll move the question.