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.
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.
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).
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.
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. 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:
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.
For Android, Use BackHandler to exit the App:
import React, { BackHandler } from 'react-native'; BackHandler.exitApp();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With