How to dismiss ActionSheet or Alert in code?

Hi,

I need dismiss ActionSheet or Alert in some scenarios like hardware back button pressed,etc.

Popover and Menu can dismiss or close in code , but ActionSheet and Alert are not.

What can I do?

You can dismiss an alert programmatically, you just have to save an instance of the alert and then call the dismiss method.

public currentAlert: Alert;
opt = {...};
let alert = this.alertCtrl.create(opts);
this.currentAlert = alert;

// alert.present() 

public clearAlert(): void {
    this.currentAlert.dismiss();//clears it
}

now you have an exposed method to clear your aert.

Can you omit

let alert = this.alertCtrl.create(opts);
this.currentAlert = alert;

and make it

this.currentAlert = this.alertCtrl.create(opts)

?

Or what’s the purpose of doing it twice?

both are same, assign alert into propriety