Get #Amazon #Prime for this #holiday #amazonprime #christmas #2019

#ReactNative - Confirm to shut down prompt / alert!

On Android, you can show a prompt to confirm with the users whether to shut down the app when they press the back button.
  1. Intercept Android BackHandler hardwareBackPress event. 
  2. Display a prompt using Alert.
ReactNative - intercept backHandler example
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
ReactNative backHandler example

Alert

ReactNative alert example
ReactNative alert example

Thank you for reading!

Jun