Alert Controller Dismiss on tapping the background

I saw a strange behavior of the Alert controller.
When tapping the background, the alert controller is dismissed (and uses the first button as action)

I tried also to write native code to see if it’s the same behavior and it’s not.

Is this a bug? It can also be seen here: https://ionicframework.com/docs/api/components/alert/AlertController/

Tapping the backdrop executes the handler for the cancel button, or dismisses the alert if the cancel button does not exist. I think it’s good practice to designate a cancel button (role: ‘cancel’), in part for this reason. But it doesn’t matter if dismissing the alert is all you need. If you need a different behavior, change the cancel button handler.

The problem is that, while having an alert, I don’t want to be able to tap anywhere in the application and the alert to be “destroyed”, I want to have an alert which prevents the user for going into the application until he clicks on the alert buttons.

Is this possible? Tried on native code and it works as expected.

As with so many other situations involving alerts, if you want to customize an alert, it’s probably better to use a modal. You can use a modal and a guard to accomplish what you want.

Solved the problem, I read again the documentation and this is done very simple, my bad for not seeing it earlier.

Property: enableBackdropDismiss boolean Whether the alert should be dismissed by tapping the backdrop. Default true.

2 Likes