Это работает как ваш вопрос,
import React, {Component} from 'react';
import {StyleSheet, Text, View,BackHandler,Alert,Modal,TouchableOpacity,TextInput} from 'react-native';
export default class App extends Component {
state={
modalVisible:false,
}
componentDidMount(){
BackHandler.addEventListener("hardwareBackPress",
this.backpress,this.backHandler)
}
backpress=()=>{
this.setState({modalVisible:!this.state.modalVisible})
return true;
}
backHandler=()=>{
BackHandler.exitApp()
}
render() {
return (
<View style={styles.container}>
<TextInput
style={{height: 40,width:200,marginLeft:50,marginVertical:20,borderBottomWidth:1}}
placeholder="Mobile Number"
onChangeText={(mobilenumber) => this.setState({mobilenumber})}
/>
<Modal
animationType="slide"
transparent={true}
visible={this.state.modalVisible}
onRequestClose={() => {
Alert.alert('Modal has been closed.');
}}>
<View style={{flex:1,justifyContent:'center',alignItems:'center',
backgroundColor:'rgba(52,52,52,0.5)'}}>
<View style={{width:300,height:250,backgroundColor:'#FFF',padding:20,
borderRadius:20,alignItems:'center',justifyContent:'center'}}>
<Text style={{fontSize:20,color:'black',alignSelf:'center'}}>Are you sure to EXIT</Text>
<View style={{flexDirection:'row'}}>
<TouchableOpacity onPress={()=>this.backpress()}
style={{padding:10,marginHorizontal:10 ,backgroundColor:'#0596C5',alignItems:'center',justifyContent:'center',borderRadius:20}}>
<Text style={{color:'white',padding:5}}>STAY</Text>
</TouchableOpacity>
<TouchableOpacity onPress={()=>this.backHandler()}
style={{padding:10,marginHorizontal:10 ,backgroundColor:'red',alignItems:'center',justifyContent:'center',borderRadius:20}}>
<Text style={{color:'#FFF',padding:5}}>EXIT</Text>
</TouchableOpacity>
</View>
</View>
</View>
</Modal>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
}
});