Ionic - back button exit app not working in Ionic 5

I am using Ionic version 5.4.16 and i am trying to close the app by hardware back button with a alert message for confirm to exit. I have checked a lot of tutorials for the same and everywhere i saw 2 ways to achieve that –

this.platform.exitApp();

and

navigator.app.exitapp()

or

navigator.[“app”].exitapp();

But none of these option are working in Ionic 5, its not recognizing the function exitApp(). So how to achieve the same if anyone has faced the same issue do suggest, thank you.

can you share minimal repo to reproduce

this.platform.backButton.subscribe(()=>{
console.log (‘exit’);
navigator[‘app’].exitApp();
});

I have used same code in my app it is working perfectly with ionic 5, Please share blank template repo with above code.

do i have to install any package for that, m i missing any npm?

brothers and sisters, pls help
error

1 Like

Try this

import { IonRouterOutlet, Platform } from ‘@ionic/angular’;
import { Plugins } from ‘@capacitor/core’;
const { App } = Plugins;

constructor(
private platform: Platform,
private routerOutlet: IonRouterOutlet
) {
this.platform.backButton.subscribeWithPriority(-1, () => {
if (!this.routerOutlet.canGoBack()) {
App.exitApp();
}
});
}

Click here for Ref