O'Reilly logo

React Native By Example by Richard Kho

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

BackAndroid

The last thing we need to handle is the back button. A universal back button, either a hardware or software implementation, is found on each Android device. We will need to use the BackAndroid API to detect back button presses and set our own custom functionality. If we don't do this, the back button will automatically close the app each time it is pressed.

To use it, we can add an event listener during the componentWillMount life cycle event that will pop the navigator when a back button press is detected. We can also remove the listener when the component is unmounted.

During componentWillMount, add an event listener to the BackAndroid API for a hardwareButtonPress event, firing _backButtonPress when triggered:

 // Tasks/app/components/EditTask/index.android.js ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required