In my ionic android app, i am using android hardware back button to navigate previous page and at last to exit app but it is not working at all, even i tried to check is there click event is working or not but there is no luck. I had gone through many solutions but it was for ionic 3 and 4 which is also not working solution. Please suggest if you have any solution for this issue
here is my code: app.component.ts file
import { Component, QueryList, ViewChild, ViewChildren } from '@angular/core';
import { AlertController, IonRouterOutlet, Platform } from '@ionic/angular';
import { SplashScreen } from '@ionic-native/splash-screen/ngx';
import { StatusBar } from '@ionic-native/status-bar/ngx';
import { ActivatedRoute, Router } from '@angular/router';
import { AuthService } from './services/auth.service';
import { CommonService } from './services/common.service';
import { environment } from 'src/environments/environment';
import {Location} from '@angular/common';
@Component({
selector: 'app-root',
templateUrl: 'app.component.html',
styleUrls: ['app.component.scss']
})
export class AppComponent {
displayUserData: any;
fname: string;
lname: string;
skill: string;
photo: string;
assetURL: string;
role_id: number;
interval: any;
@ViewChildren(IonRouterOutlet) routerOutlets: QueryList < IonRouterOutlet > ;
lastTimeBackPress = 0;
timePeriodToExit = 2000;
notification_count: any;
constructor(
private platform: Platform,
private splashScreen: SplashScreen,
private router: Router,
private statusBar: StatusBar,
private authService: AuthService,
private commonService: CommonService,
public alertController: AlertController,
private location: Location,
private activeRoute: ActivatedRoute,
) {
this.assetURL = environment.assetsUrl;
this.initializeApp();
this.loadData();
this.interval = setInterval(() => {
this.loadData();
}, 3000);
}
ngOnDestroy() {
if (this.interval) {
clearInterval(this.interval);
}
}
initializeApp() {
this.platform.ready().then(() => {
this.statusBar.styleDefault();
this.splashScreen.hide();
// this.router.navigateByUrl('splash')
this.backButtonEvent();
});
}
logout(){
// this.authService.logout();
this.LogoutConfirm();
}
async LogoutConfirm() {
const alert = await this.alertController.create({
message: 'Do you want to sign out?',
buttons: [{
text: 'No',
role: 'cancel',
cssClass: 'secondary',
handler: (blah) => {}
}, {
text: 'Yes',
handler: () => {
this.authService.logout();
}
}]
});
await alert.present();
}
loadData(){
this.authService.userData.subscribe((res: any)=>{
this.displayUserData = res;
if(this.displayUserData){
this.getDetails(this.displayUserData.token);
this.getOtherData(this.displayUserData.token);
}
})
}
getDetails(token){
this.commonService.getDetails(token).subscribe((res:any)=>{
if(res.status == 'success'){
const data = res.data;
if(data){
this.fname = data.fname;
this.lname = data.lname;
this.skill = this.displayUserData.role == 2 ? 'Makeup Designer' : data.skill;
this.photo = data.photo;
this.role_id = data.role_id
}
}else{
// this.toastService.presentToast(res.message, 'top');
}
}, (error: any) => { }
)
}
getOtherData(token){
this.commonService.getOtherData(token).subscribe((res:any)=>{
if(res.status == 'success'){
this.commonService.changeNotifyCount(res.data.notification_count);
}else{ }
}, (error: any) => { console.log(error) }
)
}
backButtonEvent() {
this.platform.backButton.subscribeWithPriority(0, () => {
console.log(this.router.url);
this.routerOutlets.forEach(async(outlet: IonRouterOutlet) => {
if (this.router.url != '/index/home') {
await this.router.navigate(['/index/home']);
} else if (this.router.url === '/index/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({
message: 'Do you want to exit the app?',
buttons: [{
text: 'Cancel',
role: 'cancel',
cssClass: 'secondary',
handler: (blah) => {}
}, {
text: 'Exit',
handler: () => {
navigator['app'].exitApp();
}
}]
});
await alert.present();
}
}