[SOLVED] How to exit the app please?

Hello,
I’m under Ionic v4, and I would like to exit the application from JavaScript.
All solutions found on this forum or on Google doesn’t seems to works:

ionic.Platform.exitApp()
=> ERROR in src/app/home/home.page.ts(69,5): error TS2304: Cannot find name ‘ionic’

this.platform.exitApp()
=> ERROR in src/app/home/home.page.ts(71,10): error TS2339: Property ‘platform’ does not exist on type ‘HomePage’.

navigator[‘app’].exitApp()
=> compilation OK, but in JS the navigator[‘app’] is undefined

I’ve also tried the plugin cordova-plugin-exit, but the author doesn’t known how this could works with Ionic v4

I need a solution that works from Android 5.0 please. Thanks for your help!

Thank for your help @gokujy but this still doesn’t works. You use one of the methods I’ve listed in my post, this one:
navigator[‘app’].exitApp()

But nothing happen when I click on my button to execute this code in my emulator :frowning:

Okey i will try later today and will let you know if i solve

1 Like

Waouhhh you’re my hero!!
This works, but WTF! Why this only works if I add this rate plugin ?!
I suspect it’s not this plugin, but the dependencies of this plugin!
THANK YOU :smiley:

1 Like

Hola @noxdigital y @gokujy que pena es que también tengo el mismo problema pero no logro conseguir que mi app se salga al presionar el botón salir de mi mobil android, ¿podrían ayudarme por fa? soy nuevo en esto :frowning:

Hello, If you still having exit app issue ping me

gokujy Please assist with exit app I tried all above mentioned solutions but still no luck. Thank you in advance

which ionic version you using?

ionic version 4.10.3
Cordova version 8.1.2 (cordova-lib@8.1.1)
NPM version 6.4.1

Hello,

add this in the app.component.ts

//code for exit app
  // set up hardware back button event.
  lastTimeBackPress = 0;
  timePeriodToExit = 2000;

  //code for exit app
  @ViewChildren(IonRouterOutlet) routerOutlets: QueryList<IonRouterOutlet>;

  constructor(
    private platform: Platform,
    private splashScreen: SplashScreen,
    private statusBar: StatusBar,
    private toastController: ToastController,
  ) {
    this.backButtonEvent();
  }

  // active hardware back button
  backButtonEvent() {
    this.platform.backButton.subscribe(async () => {

      this.routerOutlets.forEach(async (outlet: IonRouterOutlet) => {
        if (outlet && outlet.canGoBack()) {
          outlet.pop();

        } else if (this.router.url === '/home') {
          if (new Date().getTime() - this.lastTimeBackPress < this.timePeriodToExit) {
            // this.platform.exitApp(); // Exit from app
            navigator['app'].exitApp(); // work in ionic 4

          } else {
            const toast = await this.toastController.create({
              message: 'Press back again to exit App.',
              duration: 2000,
              position: 'middle'
            });
            toast.present();
            // console.log(JSON.stringify(toast));
            this.lastTimeBackPress = new Date().getTime();
          }
        }
      });
    });
  }

Add this code in home page or a specific page when user click back button to exit

public subscription: any;

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

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

Am getting Error

src/app/app.component.ts(16,45): error TS2304: Cannot find name ‘QueryList’.
[ng] src/app/app.component.ts(16,55): error TS2304: Cannot find name ‘IonRouterOutlet’.
[ng] src/app/app.component.ts(107,49): error TS2304: Cannot find name ‘IonRouterOutlet’.

ah sorry i forgot to add this lines:

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

import { IonRouterOutlet } from '@ionic/angular';

Am getting no error message and the is still no difference the app won’t exit

1 Like

Can you assist me on where I could at least find the device “back button” function

share minimal repo to reproduce via github or stackblitz

Did you add this at app.module.ts?

IonicModule.forRoot({
      hardwareBackButton: true
    }),