How do I exit/shut down a React Native app?

For Android, Use BackHandler to exit the App:

import React, { BackHandler } from 'react-native';

BackHandler.exitApp();

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;
}

There's no react-native specific way to do this today. You'd have to accomplish this from the native side of things.

Further, are you developing for iOS? Apple has stated that apps should not close themselves.


Write a native module that performs the following actions when called:

IOS:

exit(9);

ANDROID:

((YourApplication) self.getApplicationContext()).kill();

...EDIT...

Or just use the one I created: https://www.npmjs.com/package/react-native-exit-app


This is how I've achieved it:

  componentWillMount() {
    BackHandler.addEventListener('hardwareBackPress', this.handleBackButtonClick);
  }
  componentWillUnmount() {
    BackHandler.removeEventListener('hardwareBackPress', this.handleBackButtonClick);
  }
  handleBackButtonClick() {
    BackHandler.exitApp();
    return true;
  }