Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 994
Nolan Avatar asked Jan 14 '16 23:01

Nolan


People also ask

How do I exit the app react-native?

In this post I will discuss how you can implement "exiting your app when back button is pressed twice". So if your user is using your app and accidently presses the back button, the app will exit.

How do I turn off screen react-native?

I use react-native-idle-timer to turn off the screen (it just sets or removes WindowManager. LayoutParams. FLAG_KEEP_SCREEN_ON ). This works perfectly to turn off the screen (the FLAG_KEEP_SCREEN_ON is removed from the activity and then the screen goes to sleep in few minutes).

How to warn the user about exiting the app in React Native?

So to warn the user about exiting the app we can use BackHandler provided by the react native library. So the flow is when the user touches the back button it will not exit the app and when he double touches it, we will be alerting him to either go back to the app or exit the app. So below is the code to implement it.

How to close react native app programmatically using backhandler?

Let’s start today article How to close react native app programmatically React Native provides the BackHandler component to handle exit functionality in android. when you call BackHandler.exitApp (); app will close but it will remain in android’s recent tab.

How to stop a react app from running in Windows?

How to Stop a React App from Running in Windows. Let’s say you are running a React app, which you started using npm start: It is running in a browser, but closing the browser does not stop the app: To stop this from running, in the command prompt, type CTRL-C. You will get the prompt to Terminate batch job:

Can I display an alert without a button in React Native?

Apple would reject your iOS app if you exit the app on button click. You could just show an alert without a button. User won't be able to dismiss the alert and enter the app rendering it useless. your solution makes sense but the issue is that Alerts cannot be displayed without buttons in react native.


1 Answers

For Android, Use BackHandler to exit the App:

import React, { BackHandler } from 'react-native';  BackHandler.exitApp(); 
like image 63
herbertD Avatar answered Sep 28 '22 13:09

herbertD