2

so in ionic 3 there was registerBackButton() but in ionic 4 this option is no longer there and has been sitting on the shelf for quite some time now. I have read the post here that tries to solve the solution I am looking for, however, the back button still performs as it wants to. this SO answer shows another way but it is the same idea of intercepting and navigating, however, I am just, for now, trying to dismiss the top modal in the stack.

scenario: users open a search modal(modal1) which then they click on a users profile modal(modal2). The person wants to go back to the search modal(modal1) but instead of clicking the nice button that allows them to do that, they use the hardware back button.

result: all modals(modal1 and modal2) are closed.

desired effect: using the hardware back button will allow for custom navigation based on logic in place.

attempted code:

this.platform.backButton.subscribeWithPriority(0, (): void => {
  this.modalCtrl.getTop().then(
    async (value: HTMLIonModalElement): Promise<void> => {
      if (!!value) {
        await this.modalCtrl.dismiss();
      } else {
        this.navCtrl.navigateRoot('/home');
      }
    },
  );
});

also have tried :

 // registering back, if there is a view on top, close it, don't go to home.
this.platform.backButton.subscribeWithPriority(0, async (): Promise<void>=> {
  try {
    console.log('try');
    const element = await this.modalCtrl.getTop();
    if (element) {
      console.log('in true');
      await element.dismiss();
    }
  } catch (error) {
    console.log('error closing modal', error);
  }
});

note when pressing the back button I never see ANY of the console logs... maybe things have changed a lot more? since the previous Stack overflow questions.

UPDATE:

If you are having this same issue then know you are not alone!

This, and many others are well known, see here for a list they are tracking the issues. Nothing else to do... but wait... I guess...

I will update this when there is a change

Ctfrancia
  • 1,248
  • 1
  • 15
  • 39
  • As i remember , you can use navcontroller.pop() is url is not main page and if app is main then navigator.exit or something like that . – Mostafa Harb Feb 19 '20 at 18:43
  • 1
    @MostafaHarb right, however modals don't have a url to "pop" from, as they are a stack of views, – Ctfrancia Feb 19 '20 at 18:53
  • You mean you want to press back on modal and let it close or if it is closed and back os pressed then you don't what it to reopen again? – Mostafa Harb Feb 19 '20 at 19:25

0 Answers0