[英]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.