My application navigation goes like this:
- Login Page (set as Root)
- TabsPage (set as Root, once login is success)
- ListPage (ion-tab root)
- FilterPage (opens as Modal)
- DetailsPage (Page is pushed)
- SearchPage (ion-tab root)
- SettingsPage (ion-tab root)
- History (Page is pushed)
- HistoryDetailsPage (Page is pushed)
- History (Page is pushed)
- ListPage (ion-tab root)
Problem is kind of same as this question. But that didn't help me.
Problem:
Whenever I press hardware back button from pushed Pages or ion-tab root pages, I see only the ["TabsPage"] for nav.getViews() in the back button event of app.component.ts. How can I get the name of Pushed Page in that global back button event?
CODE FOR REFERENCE:
app.component.ts:
constructor(..., private platform: Platform,private modalCtrl: ModalController,private ionicApp: IonicApp,private app: App, ...) {
// ...
this.registerAction();
}
registerAction(): void {
if (this.platform.is("android")) {
this.unRegisterBackButtonAction = this.platform.registerBackButtonAction(
(event) => {
this.backButtonClick();
}
);
}
}
backButtonClick() {
let views = this.nav.getViews().map(x => x.name);
console.log(JSON.stringify(views)); // ALWAYS SHOWS ["TabsPage"], not the actual page (eg: "DetailsPage")
let activePortal = this.ionicApp._loadingPortal.getActive() ||
this.ionicApp._modalPortal.getActive() ||
this.ionicApp._overlayPortal.getActive();
if (activePortal) {
activePortal.dismiss();
}
else if(this.nav.canGoBack()) {
this.nav.pop();
} else {
if (new Date().getTime() - this.lastTimeBackPress < this.timePeriodToExit) {
this.platform.exitApp(); //Exit from app
} else {
this.alerts.showToast("Press back button again to exit");
this.lastTimeBackPress = new Date().getTime();
}
}
}