Reputation: 31
I am facing problem with the hardware back button on android. I am working with Ionic CLI 4.12.0 I want to exit the app when user on homePage and clicks back button. But back button event is not firing up. It navigate to login page then restarts the app. I am using Tabs template in my app. I have tried many answers of stackoverflow that claims solution to the similar problem. I have setup code in app component as following:
@ViewChild(IonRouterOutlet) routerOutlet: IonRouterOutlet;
constructor(private platform: Platform){
this.platform.backButton.subscribeWithPriority(0, () => {
console.log("back button clicked");
navigator["app"].exitApp();
})
}
Upvotes: 0
Views: 1576
Reputation: 61
I have simple app with menu and this is my solution: 1) in app.components.ts i have hardware back button implemented to get back on every page:
constructor(
private platform: Platform,
private splashScreen: SplashScreen,
private statusBar: StatusBar,
private nav: NavController
) {
this.initializeApp();
}
initializeApp() {
this.platform.ready().then(() => {
this.statusBar.styleDefault();
this.splashScreen.hide();
this.platform.backButton.subscribeWithPriority(1, () => {
this.nav.back();
});
});
}
2) this additional code in home.page.ts is to close app with hardware back button(look at priority):
ngOnInit() {
this.platform.backButton.subscribeWithPriority(2, () => {
console.log('BACK button pressed');
navigator['app'].exitApp();
});
}
Upvotes: 1
Reputation: 31
Finally I found the answer of my question:
ionViewDidEnter() {
document.addEventListener("backbutton",function(e) {
console.log("disable back button called from tab 1")
}, false);
}
Upvotes: 0