67

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 shut it down when the button is clicked. How do I do this?

I suspect the key is in AppRegistry but the docs are a bit scant.

Josh David Miller
  • 120,525
  • 16
  • 127
  • 95
Nolan
  • 2,209
  • 3
  • 18
  • 20

9 Answers9

112

For Android, Use BackHandler to exit the App:

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

BackHandler.exitApp();
Stanislav Mayorov
  • 4,298
  • 5
  • 21
  • 44
herbertD
  • 10,657
  • 13
  • 50
  • 77
  • 13
    `BackAndroid` has since been deprecated in favour of `BackHandler` (see https://facebook.github.io/react-native/docs/backhandler.html) – Bataleon Sep 13 '17 at 07:59
  • 18
    Note for those looking to actually "stop" their app, this doesn't "stop" the app, or stop the process from running. The app is still running in background and the user will be returned to the "desktop" – lance.dolan Aug 29 '18 at 19:23
48

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;
}
khateeb
  • 5,265
  • 15
  • 58
  • 114
Fazeel Qureshi
  • 854
  • 9
  • 18
24

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.

Adam Terlson
  • 12,610
  • 4
  • 42
  • 63
21

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

WiRa
  • 949
  • 10
  • 14
  • 4
    Caution: the referenced library uses the GPLv3 license (Source code must be made available when the software is distributed) – Jessy Apr 15 '18 at 18:25
  • 2
    There's an MIT version here: https://www.npmjs.com/package/react-native-close-app – sudo Aug 17 '19 at 19:03
  • @sudo any updates on this `react-native-exit-app` and `react-native-close-app` not removing app from background task list? – Sagar May 28 '21 at 08:17
  • @Sagar Oh, it was so long ago that I don't remember whether I ended up using that. But in iOS at least, I think there's no way to make an app remove itself from the app switcher. I think even if it crashes, it'll still be there and will reinitialize if the user switches to it. – sudo Jul 05 '21 at 07:09
9

This is how I've achieved it:

  componentWillMount() {
    BackHandler.addEventListener('hardwareBackPress', this.handleBackButtonClick);
  }
  componentWillUnmount() {
    BackHandler.removeEventListener('hardwareBackPress', this.handleBackButtonClick);
  }
  handleBackButtonClick() {
    BackHandler.exitApp();
    return true;
  }
arled
  • 2,577
  • 6
  • 27
  • 49
8

This npm module helped me with the same issue - react-native-exit-app

import RNExitApp from 'react-native-exit-app';
...
RNExitApp.exitApp();
...
RomanSorin
  • 189
  • 2
  • 7
  • 2
    I am using same but it keep app in background task it wont get killed any idea? – Sagar May 25 '21 at 05:00
  • can try npm i https://github.com/SaeedZhiany/react-native-exit-app/tree/patch-1 -S while(true){ BackHandler.exitApp(); RNExitApp.exitApp(); } – johndpope Mar 25 '22 at 00:25
2

Only For Android Exit on Backpress


import { BackHandler } from "react-native";

useEffect(() => {
  BackHandler.addEventListener("hardwareBackPress",()=>{
    BackHandler.exitApp();
  });
}, []);
Abhi 8686
  • 21
  • 2
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 02 '22 at 02:03
1

You can just throw an exception to close the app both on iOS and Android:

const exitApp = () => {
  throw {};
}

This will show you an error in debug mode, but in production mode, the app will exit itself due to the crash

Anton Liannoi
  • 543
  • 5
  • 13
1
// only works for android

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

useEffect(() => {
  setTimeout(() => {
     BackHandler.exitApp();
  }, 2000);
}, []);
Rohit Parte
  • 3,365
  • 26
  • 26
  • While this code snippet may be the solution, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Shawn Hemelstrand Jan 24 '23 at 05:17