Ionic 4 alternative for platform.registerBackButtonAction

风格不统一 提交于 2019-11-30 05:32:54

问题


I looked around the new platform for the Ionic 4, it seems like the registerBackButtonAction function was removed from it.

Are there any other alternatives to handle the Android hardware back button?


回答1:


Update: This was fixed in dfac9dc


Related: how to integrate hardware back button into ionic4 navigation


This is tracked on GitHub, in the Ionic Forums and Twitter
Until there is an official fix, you can use this workaround:

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...)
})

Be aware that you need to save the result of subscribe(...) if you ever want to unsubscribe from it again.


Old answer: (out of date as of April 2018)

registerBackButtonAction is just a wrapper for the corresponding Cordova call.

So you can just take your old call to registerBackButtonAction:

this.platform.registerBackButtonAction(() => { 
  // code that is executed when the user pressed the back button
});

and replace it with:

this.platform.ready().then(() => {
  document.addEventListener("backbutton", () => { 
    // code that is executed when the user pressed the back button
  });
});



回答2:


i tried on

"@ionic/angular": "^4.7.0-dev.201907191806.32b736e",
"@ionic/core": "^4.7.0-dev.201907191806.32b736e",

it works!

ionic git commit https://github.com/ionic-team/ionic/commit/978cc39009a9a0fb065540ce17e10c685b6c101a




回答3:


In case of @ionic/vue you can put this (or something like this) in main.js

import { Plugins } from '@capacitor/core'

Plugins.App.addListener('backButton', function() {
  console.log(111);
  window.history.back();
});

No, no, console.log(111); is not mistake, it's part of solution :)



来源:https://stackoverflow.com/questions/51728175/ionic-4-alternative-for-platform-registerbackbuttonaction

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!