Is it possible to disable the back button of the mobile in an Ionic page?

Hi,
I have an app that has a fab button on the home page that opens the plugin to read the QR codes.

If the user logs into the app, he arrives on the home page.
If he clicks on the fab button, he opens the QR code reader.
Now if he clicks on the back button of the mobile (while the QR code reader is opened) the user is redirected to the login page, not to the home page.

The same thing happens if the user logs into the app and, on the home page, clicks on the back button of the mobile. He is redirected to the login page.
If the user, before clicking the back button, has opened other pages of the app, then the back button of the mobile doesn’t lead to the login page as expected.

Now, is it possible to disable the back button of the mobile in an Ionic page?
I would like that if a user is on the home page, then the back button does not lead to the login page.

Thank you very much

cld

I don’t know if this is the best way to solve this, but I faced this same problem some time ago and solved this way:

import { HostListener } from '@angular/core';

...

// Place this somewhere in your page 
@HostListener('document:ionBackButton', ['$event'])
private async overrideHardwareBackAction($event:any) {
	await void(0); // Do nothing
}

But you can change the “Do nothing” part and put some navController method to go back to the previous page.

1 Like

Hi,
thank you for your answer.
The function overrideHardwareBackAction is actually called when I press the back button.
I’ve tried to use this to stop the action:

 $event.defaultPrevented = true;

bit doesn’t work.

So google a bit and using your suggestion I’ve found this article.

This seems to solve the problem.
So, the the code I’m using is:

  @HostListener('document:ionBackButton', ['$event'])
  private async overrideHardwareBackAction($event: any) {
    console.log('overrideHardwareBackAction:', $event);
    // $event.defaultPrevented = true;
    $event.detail.register(100, async () => {
      $event.stopImmediatePropagation();
      $event.stopPropagation();
      $event.preventDefault();
    });
    await void(0); // Do nothing
  }

Thank you again

cld