请在 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/