首页 文章

Ionic 2:处理android后退按钮

提问于
浏览
0

我试图在离子2下面处理我的android后退按钮是我的代码,它将让用户在从主页或登录页面点击时退出应用程序

let alert = this.alertCtrl.create({
            title: 'Kmart',
            message: 'Do you want exit kmart app ?',
            buttons: [
              {
                text: 'Cancel',
                role: 'cancel',
                handler: () => {
                  this.logger.info("cancel clicked")
                }
              },
              {
                text: 'ok',
                handler: () => {
                  this.platform.exitApp();
                }
              }
            ]
          });

       this.platform.registerBackButtonAction(() => {
          console.log('back button pressed')
          if (this.navCtrl.canGoBack()) {
            console.log('nav can go back')
            this.navCtrl.pop()
          } else {
            //here i have to show the alert for login and home page
            alert.present();
          } 
        }, 100);

上面的代码放在我的home.ts文件中

我无法获取当前页面名称,以便我可以让我的应用程序退出 . 当用户在家或登录屏幕上双击应用程序时如何退出应用程序 .

1 回答

  • 0

    使用

    getActive()返回当前组件检查实例的ViewController . 使用ViewChild在app.component.ts中获取navController在模板中设置 #mynav

    模板: <ion-nav #myNav [root]="rootPage"></ion-nav>

    在组件类中:

    @ViewChild('myNav') navCtrl: NavController
    //to check the condition
    let view = this.navCtrl.getActive();
    if(view instanceof HomePage)
    

    检查ionic forum discussion

相关问题