When I click the "Device Hardware Back Button" using Capacitor 3.0, I'm trying to navigate to the parent component with the code below. The device back button is working correctly, but I'm facing an issue where I can't access class members in the callback function.
Below is the code snippet:
export class ConfirmBoxComponent extends DialogContentBase implements OnInit{
constructor(public dialog: DialogRef, public myService : MyService) {
super(dialog);
}
ngOnInit(): void {
this.handleHardwareBackButton();
}
public onCancelAction(): void {
console.log("Cancel confirmed", this.dialog)
myService.closeDialog(); // closeDialog not accessible through arrow or callback functions
}
handleHardwareBackButton(){
App.addListener('backButton',function(){
this.onCancelAction() // unable to access onCancelMethod within callback
})
}
}
I am receiving a "this.onCancelAction is not a method" error. I also attempted the following code but it did not resolve the issue:
handleHardwareBackButton(){
App.addListener('backButton',function(){
this.dialog.close({ actionConfirmed : false }); //this line does not execute and no errors are shown
}.bind(this))
}
Is there something I am missing? Could you please provide guidance on how to properly access class members in a callback function?