If my React Native app fails to connect to its backend, I show an Alert with an OK button. If this happens, there\'s no point in the app continuing to run, so I\'d like to s
I am answering the question too late, but i thought the way i have chosen might help someone, so I am answering this question.
componentWillMount() {
BackHandler.addEventListener('hardwareBackPress', this.backPressed);
}
componentWillUnmount() {
BackHandler.removeEventListener('hardwareBackPress', this.backPressed);
}
backPressed = () => {
Alert.alert(
'Exit App',
'Do you want to exit?',
[
{text: 'No', onPress: () => console.log('Cancel Pressed'), style: 'cancel'},
{text: 'Yes', onPress: () => BackHandler.exitApp()},
],
{ cancelable: false });
return true;
}