我已经把android后退按钮退出我的主屏幕中的反应本机应用程序中的应用程序功能.但是当我在其他屏幕上按下android后退按钮时,它也会被调用.
componentDidMount() {
if (Platform.OS == "android") {
BackHandler.addEventListener('hardwareBackPress', this.handleBackButton);
}
this._setupGoogleSignin();
this._getUserDetails();
const { navigate } = this.props.navigation;
console.log("object url is", this.state.postsArray[0].url);
}
handleBackButton = () => {
Alert.alert(
'Exit App',
'Exiting the application?', [{
text: 'Cancel',
onPress: () => console.log('Cancel Pressed'),
style: 'cancel'
}, {
text: 'OK',
onPress: () => BackHandler.exitApp()
}, ], {
cancelable: false
}
)
return true;
}
componentWillUnmount() {
BackHandler.removeEventListener('hardwareBackPress', this.handleBackButton);
}
Run Code Online (Sandbox Code Playgroud) 我对BackHandler有一些问题,问题是
当运行应用程序并转到比方说注册屏幕并触摸我手机的背面时,他们将运行该功能并显示警报以进行确认,但是现在当我转到任何其他屏幕并触摸背面时,我需要每次返回 BackHandler.exitApp() 就回到上一个屏幕;运行,虽然我写如果路由名称是注册只是退出应用程序而不是其他屏幕
这是我的代码
import React from "react";
import {
Text,
TextInput,
ActivityIndicator,
View,
KeyboardAvoidingView,
ScrollView,
Image,
TouchableOpacity,
BackHandler,
Alert
} from "react-native";
export default class signUp extends React.Component {
constructor(props) {
super(props);
}
componentDidMount() {
BackHandler.addEventListener("hardwareBackPress", this.backPressed);
}
componentWillUnmount() {
BackHandler.removeEventListener("hardwareBackPress", this.backPressed);
}
backPressed = () => {
let { routeName } = this.props.navigation.state;
console.log("route is : " + routeName);
if (routeName == "SignUp") {
console.log("ROUTE : " + routeName);
Alert.alert(
"Exit App",
"Do you …
Run Code Online (Sandbox Code Playgroud)