[英]Device(Android) back button click detection using Ionic2/angular2
我可以使用以下代码检测设备(Android)后退按钮单击事件。 但是单击“后退”按钮后,它将返回上一级并打开确认对话框。
我如何使用ionic2避免这种现象(转到上一个屏幕)?
registerBackButtonListener() {
document.addEventListener('backbutton', () => {
let backBtnCnfirm = this.alertCtrl.create({
message: 'Do you want to close the App?',
buttons: [
{
text: 'Yes',
handler: () => {
this.platform.exitApp();
}
},
{
text: 'No',
handler: () => {
}
}
]
});
backBtnCnfirm.present();
}, false);
}
后退按钮将调用navCtrl.pop()
,因此返回上一页。 Ionic提供生命周期事件,例如viewDidEnter,viewWillEnter,viewWillLeave等。
在代码中创建此函数时:
ionViewWillLeave(){
this.alertCtrl.create({
message: 'Do you want to close the App?',
buttons: [
{
text: 'Yes',
handler: () => {
this.platform.exitApp();
}
},
{
text: 'No',
role: 'cancel'
}
]
}).present();
}
Ionic将在执行navCtrl.pop()
之前执行此操作。
注意:这将在每次页面离开时执行(因此,当手动推动或弹出页面时),但是我认为您足够聪明,可以找到解决方法(布尔检查功能)
(我不知道您的要求,但我发现按下后退按钮时显示这样的消息的用户体验不好,通常人们希望返回上一页)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.