如何在离子中听Android硬件后退按钮 [英] How to listen Android hardware back button in ionic

查看:210
本文介绍了如何在离子中听Android硬件后退按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着听android硬件后退按钮,但没有效果。

I try to listen android hardware back button,but it is no effect.

主要代码:

.run(['$ionicPlatform','$ionicHistory',function($ionicPlatform,$ionicHistory) {
     $ionicPlatform.ready(function() {
                          if(window.cordova && window.cordova.plugins.Keyboard) {
                          cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
                          }
                          if(window.StatusBar) {
                          StatusBar.styleDefault();
                          }
                          });
     $ionicPlatform.registerBackButtonAction(function (e) {
         e.preventDefault();
         $ionicHistory.nextViewOptions({
             disableAnimate: true
        });
        $ionicHistory.viewHistory().backView.go();
         return false;
       }, 100);
     }])

我的运行环境是移动浏览器.Android版本4.4.2

My running environment is mobile browser.Android version 4.4.2

推荐答案

更新:我不再使用这是因为它不可靠。此外,在最新的Ionic版本中,app.ts现在是app.component.ts。

UPDATE: I'm no longer using this as it was unreliable. Additionally, in the latest Ionic release, app.ts is now app.component.ts.

对于Ionic 2,请查看我的博客文章,了解如何解决此问题。也应该适用于Ionic 1,因为它只会召唤一个Cordova听众:

For Ionic 2, check out my blog post on how to fix this. Should also work for Ionic 1, as it's only calling a cordova listener:

http://www.codingandclimbing.co.uk/blog/ionic-2-android-back-button-13

这里是实际的帖子信息:

and here's the actual post info:

在您的app.ts中,执行以下操作以获取后退按钮按预期工作(主要是!):

  initializeApp() {
    this.platform.ready().then(() => {
      this.registerBackButtonListener();
    });
  }

  registerBackButtonListener() {
    document.addEventListener('backbutton', () => {
      var nav = this.getNav();
      if (nav.canGoBack()) {
        nav.pop();
      }
      else {
        this.confirmExitApp(nav);
      }
    });
  }


confirmExitApp(nav) {
    let confirm = Alert.create({
      title: 'Confirm Exit',
      message: 'Really exit app?',
      buttons: [
        {
          text: 'Cancel',
          handler: () => {
            console.log('Disagree clicked');
          }
        },
        {
          text: 'Exit',
          handler: () => {
            navigator.app.exitApp();
          }
        }
      ]
    });
    nav.present(confirm);
  }

  getNav() {
    return this.app.getComponent('nav');
  }



注意:



如果您收到有关应用不属于导航器属性的错误:

1)在您的应用根添加一个typings文件夹:例如: app / typings

1) Add a typings folder to your app root: e.g. app/typings

2)添加名为: pluginshackyhacky.d.ts

3)添加您需要为TypeScript扩展以进行编译的属性。:

3) Add for properties you need extended for TypeScript to compile.:

interface /*PhoneGapNavigator extends*/ Navigator {
    app: any;
}

4)将pluginshackyhacky.d.ts添加到 tsconfig.json :

4) Add the pluginshackyhacky.d.ts to the compile in the tsconfig.json:

  "files": [
    "app/app.ts",
    "app/typings/pluginshackyhacky.d.ts",
    "app/typings/phonegap.d.ts"
  ]

您可以看到我还包含了phonegap.d.ts文件,其中包含许多缺少的属性/变量,允许TypeScript编译而不会出错。

You can see that I've also included the phonegap.d.ts file which includes a lot of missing properties/variables that allows TypeScript to compile without errors.

希望这可以帮助任何有这个问题的人。

Hope this helps anyone having this problem.

干杯。

这篇关于如何在离子中听Android硬件后退按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆