Confirm on back button press

I would like to be able to cancel the backButtonPress event, but I don’t want to change its core behavior.

So, when the user is on specific pages, where poping the page would make the user lose its work, I would like to show the user a confirm message, like “if you leave now, all your work will be lost. [Leave] [Cancel]”.

I’ve found no solution for this here: registerBackButtonAction default action

How could I do this in ionic 3?

1 Like

https://ionicframework.com/docs/api/navigation/NavController/ and go down to “Lifecycle events”, I think ionViewCanLeave is what you’re looking for.

ionViewCanLeave() {
    return new Promise((resolve, reject) => {
        this.alertCtrl.create({
            enableBackdropDismiss: false,
            /* title and message etc ... */
            buttons: [{
                text: "Leave",
                handler: resolve
            },{
                text: "Stay",
                handler: reject
            }]
        }).present();
    });
}

Something along those lines

4 Likes

Thank you, this worked like a charm! :slight_smile:

1 Like

Actually, later it turned out it does work - but not like a charm :slight_smile:
It has conflict with SwipeBack, so in case a new reader needs this functionality, and also has to support SwipeBack, a workaround is needed.
The issue: