Ionic 4 - Dismiss or close Modal on platform hardware back button android

吃可爱长大的小学妹 提交于 2020-01-25 04:03:25

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!