首页 > 解决方案 > 我无法处理 Ionic 4 上的 android 后退按钮

问题描述

我是离子开发的新手。我通过以下文章尝试了越来越多的方法。最后,我在 Ionic3 Vs Ionic4 中尝试了这个处理硬件后退按钮, 请帮助我@Fabian N。

但是在设备上,我看不出后退按钮是否与代码有关……那是代码在我的情况下不起作用。:(

这是我的离子信息。

Ionic:

   Ionic CLI                     : 5.2.3 (/usr/local/lib/node_modules/ionic)
   Ionic Framework               : @ionic/angular 4.6.2
   @angular-devkit/build-angular : 0.13.9
   @angular-devkit/schematics    : 7.3.9
   @angular/cli                  : 7.3.9
   @ionic/angular-toolkit        : 1.5.1

Cordova:

   Cordova CLI       : 9.0.0 (cordova-lib@9.0.1)
   Cordova Platforms : android 8.0.0, ios 5.0.1
   Cordova Plugins   : cordova-plugin-ionic-keyboard 2.1.3, cordova-plugin-ionic-webview 4.1.1, (and 16 other plugins)

Utility:

   cordova-res : not installed
   native-run  : 0.2.8 

System:

   ios-deploy : 1.9.4
   ios-sim    : 8.0.1
   NodeJS     : v11.10.1 (/usr/local/bin/node)
   npm        : 6.7.0
   OS         : macOS Mojave
   Xcode      : Xcode 10.2 Build version 10E125

我已经在app.component.ts. 但是单击后退按钮时,我无法在真实设备上收到任何警报。

1. Test case
  this.platform.backButton.subscribe(async () => {
     alert("Fired Back Button"); 
  });

2. Test case
  this.platform.backButton.subscribe(() => {
     alert("Fired Back Button"); 
  });

3. test case
  this.platform.backButton.subscribeWithPriority(0, () => {
     alert("Fired Back Button"); 
  });



4. test case
  this.platform.backButton.subscribeWithPriority(100, () => {
     alert("Fired Back Button"); 
  });

在这种情况下 4,我尝试了诸如优先级:100、101、400、999999。但是单击 android 后退按钮时我无法收到任何警报。

但实际上后退按钮总是弹出页面。

我只需要在我的项目上处理 android 硬件后退按钮。请帮我。先感谢您。此致

标签: angulartypescriptionic-frameworkionic4ionic-native

解决方案


最后我得到了我的问题的正确答案。解决方案是Enol 的回答。我已经写在我的app.component.ts文件里了。

const event = fromEvent(document, 'backbutton');
event.subscribe(async () => {
  // logic for navigation, modal, popover, menu closing
  this.navCtrl.pop(); // I have used for my case

  ...

});

谢谢马库斯


推荐阅读