I try to Exit app from a specific page(HometabsPage) using Hardware Back Button. I use below code:
var lastTimeBackPress = 0;
var timePeriodToExit = 2000;
platform.registerBackButtonAction(() => {
let view = this.nav.getActive();
if (view.component.name == 'SignInPage' ) {
if (new Date().getTime() - lastTimeBackPress < timePeriodToExit) {
platform.exitApp(); //Exit from app
} else {
this.common.presentToast("Press back again to exit App?", "bottom");
lastTimeBackPress = new Date().getTime();
}
} else {
this.nav.pop({});
}
});
In my application there is two section SignIn and Hometabs. Above code work fine on the SignIn page.
if (view.component.name == 'SignInPage' )
But I try "HometabsPage" instead of "SignInPage" after that in all pages show the toast message.
Please help me.
@Neotrixs After login, set HomeTabsPage as your Root Page. It will prevent your app from going back to LoginPage.
For Hardware Back Button,I did it by Following methods:
/* REGISTERING BACK BUTTON TO HANDLE HARDWARE BUTTON CLICKED */
registerBackButton(){
let backButton = this.platform.registerBackButtonAction(() => {
var stackSize = this.nav.length();
if(stackSize < 1)
this.askForPressAgain();
else
this.nav.pop();
},1);
}
/*ASKING FOR PRESS BACK BUTTON AGAIN*/
askForPressAgain(){
let view = this.nav.getActive();
if (view.component.name == 'ProjectsPage' || view.component.name == 'LoginPage') {
if ((new Date().getTime() - this.lastTimeBackPress) < this.timePeriodToExit) {
this.platform.exitApp(); //Exit from app
} else {
this.toast.showBottomToast(BACK_BTN_MESSAGE);
this.lastTimeBackPress = new Date().getTime();
}
}
}
In the above code, at first I checked the Stack Size, if its less than 1, then showed the Toast for confirmation of leaving the app.
Hope it will help you or someone else.