I want to implement the BackHandler to run on one component and keep the default behavior of the hardware back button 'go to the previous screen' in the rest of my app, I have a component named 'cases.js' I want to exit the app if the user clicked the back button while this component is in the screen and to navigate back if the user is on any other component, the cases screen lays over the Login screen.
Here is what I've tried in 'cases.js' file:
componentDidMount = async () => {
await BackHandler.addEventListener('hardwareBackPress', this._closeApp())
}
componentWillUnmount = async () => {
await BackHandler.removeEventListener('hardwareBackPress', this_closeApp());
}
_closeApp = async () => {
BackHandler.exitApp();
}
but it keeps closing the app immediately.
How may I achieve that?