registerBackButtonAction working bot not proper
don’t get " page && page.isRootPage "
page.isRootPage always undefined
mention below code in app.component.ts in ready section
this.platform.registerBackButtonAction(() => { alert('Back button action called'); //called console.log("Back button action called");
let activePortal = this.ionicApp._loadingPortal.getActive() || this.ionicApp._modalPortal.getActive() || this.ionicApp._toastPortal.getActive() || this.ionicApp._overlayPortal.getActive(); alert('activePortal'+activePortal); // Result is : activePortalundefined if (activePortal) { ready = false; activePortal.dismiss(); activePortal.onDidDismiss(() => { ready = true; });
console.log("handled with portal"); return; }
if ( this.menu.isOpen()) { this.menu.close(); alert('closing menu'); console.log("closing menu"); return; }
let view = this.nav.getActive(); alert('view'+view); // Result is : [object object] let page = view ? this.nav.getActive().instance : null; console.log('pages',page); // HomePage {navCtr: Nav, menu: MenuController, nav: undefined} console.log('pages',page); // result is in attachment console.log('page.isRootPage',page.isRootPage); // alert(JSON.stringify(page)); // is comment out code is not working alert('page.isRootPage'+page.isRootPage); //// Result is : page.isRootPage undefined if (page && page.isRootPage) { alert('not root page'); console.log("Handling back button on a home page"); this.alertCtrl.create({ title: 'Confirmation', message: 'Do you want to exit?', buttons: [ { text: 'Cancel', handler: () => { } }, { text:'OK', handler: () => { this.platform.exitApp(); } } ] }).present(); } else if (this.nav.canGoBack() || view && view.isOverlay ) { alert('popping back') console.log("popping back"); this.nav.pop(); } else if (this.storage.get('hasLogin') ) { alert('Returning to home page') // Result is :Returning to home page (call last) console.log("Returning to home page"); this.nav.setRoot(HomePage); } else if (!this.storage.get('hasLogin')) { alert('Not yet logged in... exiting') console.log("Not yet logged in... exiting"); this.platform.exitApp(); } else { // alert('ERROR with back button handling') console.log("ERROR with back button handling"); }
}, 1);