In my application I open a modal and I put some values on it, and if it close, open up an alert to confirm the exit, and if clicked in ‘yes’ the user lost the data puted on the input fields, so when I press the device back button I want the same behavior.
I did a function close() on the modal, to be called when the user want to close the modal, and in app.component.ts, I used the platform.registerBackButtonAction method to register the callback that will be called every time the back button is clicked. But because the modal is opened using ‘ModalController’, I can’t check if I’m in the class of the modal. Someone have a solution?
This is my actual platform.registerBackButtonAction:
platform.registerBackButtonAction(() => {
// get current active page
let view = this.nav.getActive();
let activeView: ViewController = view;
if (activeView != null) {
if (this.nav.canGoBack()) {
this.nav.pop();
} else if (typeof activeView.instance.close === 'function')
activeView.instance.close();
}
});
This is probably overkill, but if you want extremely fine-grained control, you can create a BackButtonHandlerProvider with two main methods – setHandler and runHandler. Then each page calls setHandler in ionViewWillEnter(), and your registerBackButton calls runHandler. So every page could have a different back button handler function if you wanted.
I am also having this issue, but in my case the application closes before initiating the handler I set.
I.e I set that when I click the hardware back button, the app should show a confirmation alert before closing. But instead the app goes to background, when I re-open the app it then show me the confirmation alert.
Can some please help figure out whats wrong and I hope my explanations are clear enough
Now in my app.component.ts I have the information that I’m in my modal
platform.registerBackButtonAction(() => {
// get current active page
let view = this.nav.getActive();
let activeView: ViewController = view;
if (backButton.isActive()) {
console.log("In the modal");
//callBack (close); need a way to call my function close()
} else if (activeView != null) {
if (this.nav.canGoBack()) {
this.nav.pop();
}
}
});
The doubt is: How I will call the function close of my modal from the app.component.ts?