8

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?

Fabian N.
  • 3,807
  • 2
  • 23
  • 46
nimzz
  • 1,512
  • 2
  • 11
  • 20

3 Answers3

28

Update: This was fixed in v4.0.0-beta.8 (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
  });
});
Fabian N.
  • 3,807
  • 2
  • 23
  • 46
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/177774/discussion-on-answer-by-fabian-n-ionic-4-alternative-for-platform-registerbackb). – Bhargav Rao Aug 10 '18 at 05:42
  • nice one, i really looking for this everywhere and it seems this is the true workaround – Xinez Feb 17 '19 at 00:32
  • @Xinez In case you are currently migrating and thinking about how to navigate back: since angular routing is now used its just `location.back()` from @angular/common **HOWEVER** that seems to break the back animation so stay with `navController.back()` – Fabian N. May 17 '19 at 14:11
  • backButton.subscribe stopped working in ionic 4, don't understand why?? – Ankit Maheshwari Jul 19 '19 at 10:44
  • @AnkitMaheshwari which version? So much broke an was fixed again, I didn't try it for some time so no idea if it still works in 4.6.2 – Fabian N. Jul 19 '19 at 10:57
  • Ionic CLI : 5.2.3 (/usr/local/lib/node_modules/ionic) Ionic Framework : @ionic/angular 4.6.2 @angular-devkit/build-angular : 0.13.9 @angular-devkit/schematics : 7.2.4 @angular/cli : 7.3.9 @ionic/angular-toolkit : 1.4.1 – Ankit Maheshwari Jul 19 '19 at 11:02
1

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

biubiushun
  • 11
  • 1
0

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 :)

  • 2
    Please [edit] this to explain why the `console.log` call is so important. – AdrianHHH Oct 11 '19 at 06:42
  • This is the second guy I'm seeing put a `console.log` in an app and I'm still wondering what magic trick this is. How are you guys able to see that log when the app is running on a device? – Dev Yego Oct 19 '20 at 16:41
  • Use chrome://inspect @DevYego. I think console.log delays the execution of the code and prevents a race condition, I had to use this approach in some Ionic code too. Usually a setTimeOut(..., 100) works too. – Renato Probst Jan 18 '21 at 13:22