On Android, you can show a prompt to confirm with the users whether to shut down the app when they press the back button.
- Intercept Android BackHandler hardwareBackPress event.
- Display a prompt using Alert.
 |
ReactNative - intercept backHandler example |
Sample code (Note that I used
React Native router flux)
import { Alert, BackHandler } from 'react-native'
...
showExitAlert() {
Alert.alert(
'Shut Down',
'Do you want to shut down Me? :(',
[
{
text: 'Cancel', onPress: () => {
}, style: 'cancel'
},
{
text: 'Shut down', onPress: () => {
BackHandler.exitApp()
}
},
],
{cancelable: false}
)
}
onBackPress() {
this.showExitAlert();
return true;
}
render() {
return (
<Provider store={this.props.store}>
<Router
backAndroidHandler={this.onBackPress.bind(this)}
scenes={scenes}
/>
</Provider>
);
}
BackHandler
 |
ReactNative backHandler example |
Alert
 |
ReactNative alert example |
Thank you for reading!
Jun
Comments
Post a Comment