7

I am devloping a mobile app using react native and i want to exit the app after clicking a button.

My scenario is that if the user has not verified his/her email after certains days i will prompt the user an Alert Dialog every the time the app is opened blocking the app usage until the user email is verified.

So after the users click OK, how do I exit the app programmatically?

Nahum Fabian
  • 180
  • 1
  • 2
  • 15

6 Answers6

7
import BackHandler from 'react-native';
// Note: work only on Android
    BackHandler.exitApp();
Maher Aldous
  • 894
  • 1
  • 10
  • 14
6

For iOS, you can use the following library: https://github.com/wumke/react-native-exit-app. It uses native library to exit the app programmatically. You can exit the app using:

RNExitApp.exitApp()

For android, you can use BackAndroid from https://facebook.github.io/react-native/docs/backandroid.html

BackAndroid.exitApp()
Provash
  • 1,892
  • 3
  • 20
  • 28
4

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.

Irfan Ayaz
  • 797
  • 8
  • 16
  • 1
    your solution makes sense but the issue is that Alerts cannot be displayed without buttons in react native. By default a button is included and there is no way to hide it base on what i have read so far. do you know how to show an alert without buttons? – Nahum Fabian Dec 30 '16 at 17:06
3

BackAndroid should be able to help you. Use Alert callback to use BackHandler

var {
  Alert,
  BackHandler,
} = ReactNative;



Alert.alert(
            'Alert Title',
            alertMessage,
            [
              {text: 'OK', onPress: () => BackHandler.exitApp()},
            ]
);
Bhavya Koshiya
  • 1,262
  • 2
  • 8
  • 22
coder hacker
  • 4,819
  • 1
  • 25
  • 50
1
<Text style = {something}
        onPress ={
        ()=>{
        console.log('clicked');
        return BackHandler.exitApp();
        }
        }>Exit</Text>
  • While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. – Badacadabra Jun 19 '17 at 14:01
0

Use exitApp() method from BackHandler class to exit your app in android.

import { View, TouchableOpacity, Text, BackHandler } from 'react-native';

class Scan extends Component {
    render() {
        return (
            <View>
                <TouchableOpacity onPress={()=> BackHandler.exitApp()}>
                    <Text>Quit</Text>
                </TouchableOpacity>
            </View>
        )
    }
}
Codemaker2015
  • 12,190
  • 6
  • 97
  • 81