I had posted this on another thread but the thread is dead. Just a simple example of handling the hardware back button, free of charge.
It was a somewhat frustrating process figuring it out so I hope it saves someone from a mild migraine.
I just take into account whether or not the app will exit from the page I’m setting the backButtonAction on. If so, I simply use return; as the action
export class HomePage {
private unRegisterBackButtonAction: Function;
constructor() { }
ionViewDidLoad() {
// Or on ionViewWillLoad
this.registerAction();
}
registerAction(): void {
if (this.platform.is('android')) {
this.unRegisterBackButtonAction = this.platform.registerBackButtonAction(() => {
return;
});
}
}
unRegister() {
this.unRegisterBackButtonAction && this.unRegisterBackButtonAction();
}
ionViewWillLeave() {
this.unRegister();
}
If I want to pop navigation
registerAction(): void {
if (this.platform.is('android')) {
this.unRegisterBackButtonAction = this.platform.registerBackButtonAction(() => {
this.navCtrl.pop();
});
}
}
etc…
And I do add a new registerBack action to each page. I also do not use priority codes, no need to worry about them.