To close the app from particular page on single tap of hardware back button
I am posting the solution
which worked for me, It may help for other users.
app.component.ts file should look like this:
import { Component } from '@angular/core';
import { Platform, ToastController } from '@ionic/angular';
@Component({
selector: 'app-root',
templateUrl: 'app.component.html'
})
export class AppComponent {
counter = 0;
constructor(
public platform: Platform,
public toastController: ToastController
) {
this.platform.ready().then(() => {
document.addEventListener('backbutton', async () => {
if (this.counter === 0) {
this.counter++;
this.alertToast();
setTimeout(() => { this.counter = 0; }, 3000);
} else {
(navigator as any).app.exitApp();
}
});
});
}
async alertToast() {
const toast = await this.toastController.create({
message: 'Press again to exit',
duration: 300,
position: 'middle',
});
toast.present();
}
}