registerBackButtonAction " page.isRootPage " is always undefined

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);


What is this.storage?

this.storage.get(‘hasLogin’) is store user login or not.
before login “hasLogin” is nothing after login it will store true value for check user already logdin.

That’s not what I asked. I asked what this.storage is. If it’s an instance of ionic-storage’s Storage, then get is returning Promises, and your code which is testing them for truthiness is not going to be doing what you seem to think it will.

2 Likes

okay cool,
you simply say page.isRootPage why undefined

and how to know i’m in root page or not?

use canGoBack() method of navController… It returns false when it is on root.