Control Button Hardware Back Android with Ionic 3

0

Hello

Implemented control of the physical back button for Android. I had to do this because by browsing the menu it makes the page as root and exits directly from the app by pressing it. After the first version stopped coming out of the modals. I looked at the internet and found some things and adjusted them as I needed them. But now I have one problem left, which I can not solve. When clicking on an ion-select component, the options are opened, and there you go back, it comes back from the modal leaving the options open on the screen.

 //Controle botão voltar celular
      let lastTimeBackPress = 0;
      let timePeriodToExit = 2000;

      this.platform.registerBackButtonAction(() => {

        //fecha quando keyboard aberto
        if (this.keyboard.isOpen()) {
          this.keyboard.close();
          return;
        }

        //fecha o menu se aberto
        if (this.menuCtrl.isOpen()) {
          this.menuCtrl.close();
          return;
        }

        let activePortal = this.ionicApp._loadingPortal.getActive() ||
          this.ionicApp._modalPortal.getActive() ||
          this.ionicApp._toastPortal.getActive() ||
          this.ionicApp._overlayPortal.getActive();
          
        //Fecha modals,toast,overlay
        if (activePortal) {
          activePortal.dismiss();
          //activePortal.onDidDismiss(() => { /*executa algo ao fechar*/ });
          return;
        }

        //Controle demais páginas
        let view = this.nav.getActive();
        let page = view.instance;

        if (this.nav.canGoBack() || view && view.isOverlay) {
          this.nav.pop();
        }else{
          if(!(page instanceof DashboardPage) ){
            if(this.tokenService.getToken() != null){
              this.nav.setRoot('DashboardPage');
            }else{
              this.platform.exitApp();
            }
          }else{
             //Double check to exit app
            if (new Date().getTime() - lastTimeBackPress < timePeriodToExit) {
              this.platform.exitApp(); //Exit from app
            } else {
              this.toastService.toast("Toque novamente para sair");
              lastTimeBackPress = new Date().getTime();
            }
          }
        }

        return;

      }, 0);

    
asked by anonymous 17.09.2018 / 22:15

0 answers