ionic3 - 在 Ionic 3 与 Ionic 4 中处理硬件后退按钮

标签 ionic3 ionic4

请在 ionic3 中找到以下 Android 硬件返回按钮操作的代码.如Ionic4使用角度路由进行导航 如何为后退按钮发生弹出事件?如果我们想跳到最后一页可以使用下面的代码this.navCtrl.goBack('/products'); .
但是我们如何将它用于 ionic4 中的 android 硬件后退按钮操作?

Ionic3 硬件后退按钮 Action

this.platform.registerBackButtonAction(() => {
    let activePortal = this.ionicApp._loadingPortal.getActive() ||
        this.ionicApp._modalPortal.getActive() ||
        this.ionicApp._toastPortal.getActive() ||
        this.ionicApp._overlayPortal.getActive();
    if (activePortal) {
        activePortal.dismiss();
    } else {
        if (this.nav.canGoBack()) {
            ***this.nav.pop();***
        } else {
            if (this.nav.getActive().name === 'LoginPage') {
                this.platform.exitApp();
            } else {
                this.generic.showAlert("Exit", "Do you want to exit the app?", this.onYesHandler, this.onNoHandler, "backPress");
            }
        }
    }
});

最佳答案

这是我在 上的工作代码 ionic 5 项目 .
使用 Cordova/PhoneGap

import {Component} from '@angular/core';
import {ToastService} from './_services/toast.service';

@Component({
  selector: 'app-root',
  templateUrl: 'app.component.html',
  styleUrls: ['app.component.scss']
})
export class AppComponent {
  constructor(private toastCtrl: ToastService) {
    this.backButton();
  }
  backButton() {
      const that = this;
      let lastTimeBackPress = 0;
      const timePeriodToExit = 2000;
      function onBackKeyDown(e) {
          e.preventDefault();
          e.stopPropagation();
          if (new Date().getTime() - lastTimeBackPress < timePeriodToExit) {
              navigator.app.exitApp();
          } else {
              that.presentToast();
              lastTimeBackPress = new Date().getTime();
          }
      }
      document.addEventListener('backbutton', onBackKeyDown, false);
  }
  presentToast() {
    const toast = this.toastCtrl.create({
      message: "Press again to exit",
      duration: 3000,
      position: "middle"
    });
  toast.present();
  }
}


或看到这个: -
https://thaaimozhikalvi.com/2020/05/28/ionic-5-double-tab-back-button-to-exit-using-cordova-phonegap/

关于ionic3 - 在 Ionic 3 与 Ionic 4 中处理硬件后退按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51700879/

相关文章:

ios - 为什么在 ionic4 的 xcode 中会出现此错误

angular - Ion-item-option 按钮需要点击两次

javascript - 无法绑定(bind)到 'datasets',因为它不是 'canvas' 的已知属性

angular - 无法读取未定义的sqlite ionic-v3和SQLite的属性“split”

node.js - 找不到模块 node_modules\@ionic\app-scripts IONIC 3

android - 向 F-Droid 贡献 Ionic 3 应用程序

javascript - ionic 3 : Using result from JSON response in second GET call

ionic-framework - 如何在 ionic 日期时间中设置最小和最大时间

javascript - 预加载、延迟加载页面 Ionic 4

ios - 有没有办法在我的 PC 上以 iOS 模式测试我的 Ionic 应用程序?