Android 上的 Ionic 4 应用程序没有硬件后退按钮

Hardware back button doesn't the app on Android with Ionic 4

我在 android 上遇到硬件后退按钮的问题。我正在使用 Ionic CLI 4.12.0 当用户在主页上并单击后退按钮时,我想退出应用程序。但是后退按钮事件没有启动。它导航到登录页面,然后重新启动应用程序。我在我的应用程序中使用 Tabs 模板。我已经尝试了很多声称可以解决类似问题的 Whosebug 答案。我在应用程序组件中设置代码如下:



@ViewChild(IonRouterOutlet) routerOutlet: IonRouterOutlet;

  constructor(private platform: Platform){
      this.platform.backButton.subscribeWithPriority(0, () => {
      console.log("back button clicked");
      navigator["app"].exitApp();
})
}

终于找到问题的答案了:


  ionViewDidEnter() {
    document.addEventListener("backbutton",function(e) {
      console.log("disable back button called from tab 1")
    }, false);
}

我有带菜单的简单应用程序,这是我的解决方案: 1) 在 app.components.ts 中,我实现了硬件后退按钮以返回到每一页:

constructor(
    private platform: Platform,
    private splashScreen: SplashScreen,
    private statusBar: StatusBar,
    private nav: NavController
  ) {
    this.initializeApp();
  }

initializeApp() {
    this.platform.ready().then(() => {
      this.statusBar.styleDefault();
      this.splashScreen.hide();
      this.platform.backButton.subscribeWithPriority(1, () => {
        this.nav.back();
      });
    });
  }

2) home.page.ts 中的附加代码是使用硬件后退按钮关闭应用程序(查看优先级):

  ngOnInit() {
    this.platform.backButton.subscribeWithPriority(2, () => {
      console.log('BACK button pressed');
      navigator['app'].exitApp();
    });
  }