gpt4 book ai didi

android - ionic 2 : How to handle the hardware back button which checks confirmation of Exit in app

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:51:28 27 4
gpt4 key购买 nike

我面临一个问题,如何处理默认手机的后退按钮,该按钮在退出应用程序时检查确认,如果我按下后退按钮,应该调用一些显示弹出窗口的处理程序,以确认退出。或者有任何方法调用 registerBackButtonAction()?或者是如何在 IONIC 2 中使用它?请帮帮我。提前致谢。

最佳答案

在 app.component.ts 中

        @ViewChild(Nav) nav: Nav;

constructor(private platform: Platform, private toastCtrl: ToastController, private alertCtrl: AlertController) {
platform.ready().then(() => {
// Okay, so the platform is ready and our plugins are available.
// Here you can do any higher level native things you might need

platform.registerBackButtonAction(() => {


//uncomment this and comment code below to to show toast and exit app
// if (this.backButtonPressedOnceToExit) {
// this.platform.exitApp();
// } else if (this.nav.canGoBack()) {
// this.nav.pop({});
// } else {
// this.showToast();
// this.backButtonPressedOnceToExit = true;
// setTimeout(() => {

// this.backButtonPressedOnceToExit = false;
// },2000)
// }

if(this.nav.canGoBack()){
this.nav.pop();
}else{
if(this.alert){
this.alert.dismiss();
this.alert =null;
}else{
this.showAlert();
}
}
});
});

}

showAlert() {
this.alert = this.alertCtrl.create({
title: 'Exit?',
message: 'Do you want to exit the app?',
buttons: [
{
text: 'Cancel',
role: 'cancel',
handler: () => {
this.alert =null;
}
},
{
text: 'Exit',
handler: () => {
this.platform.exitApp();
}
}
]
});
alert.present();
}

showToast() {
let toast = this.toastCtrl.create({
message: 'Press Again to exit',
duration: 2000,
position: 'bottom'
});

toast.onDidDismiss(() => {
console.log('Dismissed toast');
});

toast.present();
}

关于android - ionic 2 : How to handle the hardware back button which checks confirmation of Exit in app,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42151944/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com