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 ... import { BackAndroid, ... } from 'react-native'; ... export default class EditTask extends Component { ... componentWillMount () { BackAndroid.addEventListener('hardwareButtonPress', () => ...