App closes when the back button is pressed in ionic 5 android while the modal is opened

Viewed 473

The app closes unexpectedly in ionic. i'm using ionic v-5 and codova v-10. i was trying to close the modal when the android hardware back button is pressed, but the app closes completely unexpectedly instead. i tried with the surfing the ionic forums for cordova the solution there dint worked for me.

i tried this piece of code in both app component and the modal component

 this.platform.backButton.subscribeWithPriority(10, () => {
      console.log('angular Handler was called!');
      this.modal.dismiss();
    });

and

 this.platform.backButton.subscribe(() => {    
        console.log("back button pressed")
        if(this.modal.top())
        {
          this.modal.dismiss();
        }
    });

Even though trying these both code it didn't worked still the app was closed when the back button is pressed. I'm trying this from very long time but not able find the solution!

1 Answers

put this code inside function initializeBackButtonCustomHandler()

and call function inside initializeApp() this function

    this.platform.backButton.subscribeWithPriority(0, async () => {
      this.alert.dismissLoading();
      try {
        const element = await this.modalCtrl.getTop();
        if (element) {
          element.dismiss();
          return;
        }
      } catch (error) {
      }
});
Related