@ViewChildren(IonRouterOutlet) routerOutlets: QueryList;
lastTimeBackPress = 0;
timePeriodToExit = 2000;
constructor(
private router: Router,
private alertController: AlertController,
private location: Location,
private platform: Platform,
) {
this.backButtonEvent();
}
backButtonEvent() {
this.platform.backButton.subscribeWithPriority(10, () => {
alert('back button pressed');
this.routerOutlets.forEach(async (outlet: IonRouterOutlet) => {
if (this.router.url != '/home') {
// await this.router.navigate(['/']);
await this.location.back();
} else if (this.router.url === '/home') {
if (new Date().getTime() - this.lastTimeBackPress >= this.timePeriodToExit) {
this.lastTimeBackPress = new Date().getTime();
this.presentAlertConfirm();
} else {
navigator['app'].exitApp();
}
}
});
});
}
async presentAlertConfirm() {
const alert = await this.alertController.create({
// header: 'Confirm!',
message: 'Are you sure you want to exit the app?',
buttons: [{
text: 'Cancel',
role: 'cancel',
cssClass: 'secondary',
handler: (blah) => { }
}, {
text: 'Close App',
handler: () => {
navigator['app'].exitApp();
}
}]
});
await alert.present();
}
1 post - 1 participant