IONIC 4: Device back button not exiting the app

I’ve been trying to close the app with the device back button but instead of, it goes to a white screen and then go back to the home page.
I tried this:

import { Component } from '@angular/core';
import { Platform } from '@ionic/angular';

@Component({
  selector: 'app-home',
  templateUrl: 'home.page.html',
  styleUrls: ['home.page.scss'],
})
export class HomePage {

  constructor(private platform: Platform) {}

  subscription: any;

  ionViewDidEnter() {
    this.subscription = this.platform.backButton.subscribe(() => {
      navigator['app'].exitApp();
    });
  }

  ionViewWillLeave() {
    this.subscription.unsubscribe();
  }
}

It seems to work to some people, there’s something wrong i’m doing?

Check this issue and try this.

1 Like

I did it!
That was easier than I thought, just added this code in the constructor of home.page.ts:

constructor(private platform: Platform) {
    document.addEventListener("backbutton", async () => {

      navigator['app'].exitApp();

    });
  }