registerBackButtonAction

registerBackButtonAction

我环顾了Ionic 4的新平台,似乎已从其中删除了registerBackButtonAction功能。

还有其他替代方法可以处理Android硬件后退按钮吗?

最佳答案

更新:此问题已在v4.0.0-beta.8 (dfac9dc)中修复



Related: how to integrate hardware back button into ionic4 navigation



这是在GitHubIonic 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月已过期)

registerBackButtonActionjust a wrappercorresponding 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
  });
});

08-07 09:38