Currently, I'm exploring the documentation in search of a solution for the following scenario:
Imagine the user is on a form page and decides to go back. I want to prompt a verification message like
"Are you sure you want to discard your changes?"
. Depending on whether they click yes or no, a function will be executed before leaving the page.
I am aware that I can utilize
Platform.registerBackButtonAction()
to monitor when the hardware back button is clicked. However, how can I achieve similar functionality for the NavController back button located in the header?
While NavGuards are automatically integrated with the NavController back button, they execute the function after leaving the page, which does not align with my requirements.
Essentially, the desired flow is as follows:
Enter the page >> Enter information in any input field >> If attempting to leave the page, trigger a prompt alert >> Upon clicking 'yes', exit the page
Below is a snippet of the code I have been working on:
ionViewCanLeave() {
let confirmationAlert = this.alerts.create({
title: "Confirmation Message",
buttons: [{
text: 'Cancel',
handler: () => {
this.navCtrl.pop();
}
}, {
text: 'Yes',
handler: () => {
this.saveDescription();
}
}]
});
if (this.changes != undefined && this.changes!= '') {
confirmationAlert.present();
} else {
this.navCtrl.pop();
}
}
How can I implement this functionality? Is there a way to prevent users from leaving the view, perform an action, and based on the outcome, decide whether to exit?
Thank you :)