3
votes

In ionic 4, on hardware back button press I want to close or dismiss any modal that will be present. I have used the below given code, here the condition "if(modal) { modal.dismiss(); }" always get satisfied even if previous modal are dismissed

constructor(private platform: Platform, private modalCtrl: ModalController) {
this.initializeapp();
}
initializeapp() {
this.platform.registerBackButtonAction(1, async () => {
    const modal = await this.modalCtrl.getTop();
    if (modal) {
        modal.dismiss();
    }
});
}
1
Got one solution: I have set the priority to 0 i.e. this.platform.registerBackButtonAction(0, async () => { const modal = await this.modalCtrl.getTop(); if (modal) { modal.dismiss(); } }); - Remember Me

1 Answers

0
votes

Did some digging around and Ionic registers a custom event ionBackButton for handling the hardware back button press:

And the overlay code that manages all overlays in Ionic handles this ionBackButton event by dismissing the topmost overlay:

So what is your code trying to achieve?

It seems that it only does this when backdropDismiss is also set to true:

if (lastOverlay && lastOverlay.backdropDismiss) {

Which I assume means that it's classed as an optional/low-grade overlay, rather than simulating a modal.