How to dismiss all popups at the platform resume?

2019-09-13 19:30发布

In my application, I have this alert:

let confirm = this.alertCtrl.create({
                    title: 'Blabla',
                    message: 'blablabla',
                    buttons: [button1, button2, button3],
                    enableBackdropDismiss: false
                });
                confirm.present();

Also, when the application is resumed, the application should redirect to login page. No problem in making that work.

The problem comes, when I open the popup and resume the application. The application does redirect correctly, but the popup remains opened.

Is there a solution to programatically close all the popups opened? The resume function is called from app.component:

this.platform.resume.subscribe(() => {});

For this reason I cannot call confirm variable and close this popup.

3条回答
叼着烟拽天下
2楼-- · 2019-09-13 20:15

Open the alerts from common page(provider) by calling presentAlert() method.

And in onresume subscription, call the dismissAlert() method to close all opened alerts.

alerts: Alert[] = [];

 presentAlert() {
        let confirm = this.alertCtrl.create({
            title: 'Blabla',
            message: 'blablabla',
            buttons: [button1, button2, button3],
            enableBackdropDismiss: false
        });
        this.alerts.push(confirm);
        confirm.present();
    }

    dismissAlert() {
        console.log('Dismissed alert');
        if (this.alerts.length) {
            this.alerts.forEach(e => {
                e.dismiss();
            });
        }
        this.alerts = [];
    }
查看更多
戒情不戒烟
3楼-- · 2019-09-13 20:16

You can subscribe to the same event from different places. So, you can call this.platform.resume.subscribe(() => {}); from the component where your confirm variable is accessible.

查看更多
闹够了就滚
4楼-- · 2019-09-13 20:34

I can't add a comment on Sabari's answer because my rep isn't high enough yet but I can confirm that his method works. I am currently implementing methods in my components to allow the app to go back in its steps when the user presses the device's Back button, but instances of AlertController, AlertSheetController and similar were problematic.

The only change that I made in comparison to his solution was to implement a boolean called logoutStarted that changed to true whenever AlertController presented an alert.

if (this.alerts.length > 0 || this.logoutStarted) { 
        this.alerts.forEach(i => {
            i.dismiss();
            this.alerts = [];
            this.logoutStarted = false;
        });
      }

Doing this, I was able to shut down the alert from the method this.platform.registerBackButtonAction(() =>{}

查看更多
登录 后发表回答