Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ionic 4 alternative for platform.registerBackButtonAction

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?

like image 338
nimzz Avatar asked Aug 07 '18 13:08

nimzz


People also ask

How do you handle hardware back button in ionic 4?

The hardware back button is found on most Android devices. In native applications it can be used to close modals, navigate to the previous view, exit an app, and more. By default in Ionic, when the back button is pressed, the current view will be popped off the navigation stack, and the previous view will be displayed.

What platforms does ionic support?

Ionic apps are truly cross-platform: able to run as an Android, iOS, Electron, and Progressive Web App (PWA), all from a single codebase.

What is ionic platform?

The Platform service can be used to get information about your current device. You can get all of the platforms associated with the device using the platforms method, including whether the app is being viewed from a tablet, if it's on a mobile device or browser, and the exact platform (iOS, Android, etc).


2 Answers

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
  });
});
like image 80
Fabian N. Avatar answered Sep 18 '22 14:09

Fabian N.


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

like image 38
biubiushun Avatar answered Sep 18 '22 14:09

biubiushun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!