Close modal , toast, alert if android device back button clicked


#1

hi guys i want when device back button clicked if modal is open close it


#2

Any update on this question.


#3

Check this:


You’d be calling -> this.yourModal.dismiss()


#4

In your modal component constructor:

// register back button to close this modal
this.platform.registerBackButtonAction(() => {
    this.viewCtrl.dismiss();
});

In the page component where you call to open this modal:

// when modal is closed register back button to go back (default back button action)
let modalInstance = this.modalCtrl.create(ModalComponent);

modalInstance.onDidDismiss(() => {
   this.platform.registerBackButtonAction(() => {
    // if has history page go back  
    if (this.navCtrl.length() > 1) {
        this.navCtrl.pop();
      }
    });
});

modalInstance.present();