我环顾了Ionic 4的新平台,似乎已从其中删除了registerBackButtonAction
功能。
还有其他替代方法可以处理Android硬件后退按钮吗?
最佳答案
更新:此问题已在v4.0.0-beta.8 (dfac9dc)中修复
Related: how to integrate hardware back button into ionic4 navigation
这是在GitHub和Ionic Forums中的Twitter上跟踪的
在没有正式修复之前,您可以使用以下解决方法:
this.platform.backButton.subscribe(() => {
// code that is executed when the user pressed the back button
})
// To prevent interference with ionic's own backbutton handling
// you can subscribe with a low priority instead
this.platform.backButton.subscribeWithPriority(0, () => {
// code that is executed when the user pressed the back button
// and ionic doesn't already know what to do (close modals etc...)
})
请注意,您需要保存
subscribe(...)
的结果如果您想再次取消订阅。
旧答案:(截至2018年4月已过期)
registerBackButtonAction
是just a wrapper的corresponding Cordova call。因此,您可以将旧电话转到
registerBackButtonAction
:this.platform.registerBackButtonAction(() => {
// code that is executed when the user pressed the back button
});
并替换为:
this.platform.ready().then(() => {
document.addEventListener("backbutton", () => {
// code that is executed when the user pressed the back button
});
});