Please find the below code for the Android hardware back button action in ionic3
. As Ionic4
uses angular routing for navigation how the pop event will take place for the back button? If we want to pop to the last page we can use the following code this.navCtrl.goBack('/products');
.
But how we can use it for the android hardware back button action in ionic4
?
this.platform.registerBackButtonAction(() => {
let activePortal = this.ionicApp._loadingPortal.getActive() ||
this.ionicApp._modalPortal.getActive() ||
this.ionicApp._toastPortal.getActive() ||
this.ionicApp._overlayPortal.getActive();
if (activePortal) {
activePortal.dismiss();
} else {
if (this.nav.canGoBack()) {
***this.nav.pop();***
} else {
if (this.nav.getActive().name === 'LoginPage') {
this.platform.exitApp();
} else {
this.generic.showAlert("Exit", "Do you want to exit the app?", this.onYesHandler, this.onNoHandler, "backPress");
}
}
}
});
Update: This was fixed in v4.0.0-beta.8 (dfac9dc)
Related: ionic4 replacement for registerBackButtonAction
This is tracked on GitHub, in the Iconic Forum and Twitter
Until there is an official fix, you can use the workaround below.
Using platform.backButton.subscribe
(see here)platform.backButton.subscribeWithPriority(0, ...)
to let ionic handle closing all the modals/alerts/...