Problem with registerbackbutton action

How to call a function that is inside my “ConfigPage” class after the action registerbutton is pressed inside the “ConfigPage”;

this.platform.registerBackButtonAction(() => {

        let activePortal = this.ionicApp._loadingPortal.getActive() ||
        this.ionicApp._modalPortal.getActive() ||
        this.ionicApp._toastPortal.getActive() ||
        this.ionicApp._overlayPortal.getActive();

        let view = this.nav.getActive();
        let currentRootPage = view.component;


        if (activePortal) {
          activePortal.dismiss();
        }
        else if (this.menuCtrl.isOpen()) {
          this.menuCtrl.close();
        }
        else if (this.nav.canGoBack() || view && view.isOverlay) {
          this.nav.pop();
        }
        else if (currentRootPage == HomePage) {
          this.appMinimize.minimize().then(
            success => console.log('Fechado'),
            err => console.log('Algo errado:')
          );
        } else if (currentRootPage == OrcamentoPage) {
          this.platform.exitApp();
        }else if (currentRootPage == PreferenciasPage) {
          this.nav.setRoot(OrcamentoPage);
        }else if (currentRootPage == CargaPage) {
          this.nav.setRoot(OrcamentoPage);
        }else if (currentRootPage == EnviarOrcamentoPage) {
          this.nav.setRoot(OrcamentoPage);
        } else if (currentRootPage == CarrinhoPage) {
          this.nav.setRoot(OrcamentoPage);
        } else if (currentRootPage == FinalizaPage) {
          this.nav.setRoot(OrcamentoPage);
        } else if (currentRootPage == ConfigPage) {
          this.nav.setRoot(OrcamentoPage);
        } else if ( currentRootPage == PedidosPage) {
          this.nav.setRoot(OrcamentoPage)
        }
      });

You should never be directly accessing any property that begins with an underscore from outside of the class that declares it. They are private.

I also absolutely hate giant monolithic decision chains like this. I would break all of this up into separate back button handlers that are registered and unregistered using page lifecycle events.

1 Like