问题
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:
Did some digging around and Ionic registers a custom event ionBackButton
for handling the hardware back button press:
- https://github.com/ionic-team/ionic/blob/master/core/src/utils/hardware-back-button.ts#L20
And the overlay code that manages all overlays in Ionic handles this ionBackButton
event by dismissing the topmost overlay:
- https://github.com/ionic-team/ionic/blob/master/core/src/utils/overlays.ts#L66
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.
来源:https://stackoverflow.com/questions/57024833/ionic-4-dismiss-or-close-modal-on-platform-hardware-back-button-android