如何在一个屏幕中处理退出应用程序而不是整个应用程序 - React Native? [英] How to handle exit app in one screen not in the whole app - React Native?
问题描述
我遇到了 BackHandler 的问题,问题是
I have some issue with BackHandler, the issue is
运行应用时转到让我们说注册屏幕和触摸在我的手机后面,他们将运行该功能并显示警报以确认,但现在当我转到任何其他屏幕并触摸背面时,我将需要在每个背面返回到前一个屏幕BackHandler.exitApp();运行,虽然我写道如果路径名是SignUp只退出应用程序而不是其他屏幕
when running the app and go to the let's say Sign Up screen and touch the back in my mobile they will run the function and show the Alert to confirm, but now when I go to any other screen and touch the back they will I need to just back to the previous screen on every back BackHandler.exitApp(); runs, Although I write if the routname is SignUp just exit the app not other screens
这是我的代码
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 want to exit?",
[
{
text: "No",
onPress: () => console.log("Cancel Pressed"),
style: "cancel"
},
{ text: "Yes", onPress: () => BackHandler.exitApp() }
],
{ cancelable: false }
);
return true;
} else {
return false;
}
};
render() {....}
}
路由
Routes
import { createStackNavigator, createAppContainer } from "react-navigation";
import React from "react";
import { View } from "react-native";
import Splash from "../screens/Splash";
import Home from "../screens/Home";
import SignUp from "../screens/SignUp";
import SignIn from "../screens/SignIn";
import ForgetPassword from "../screens/ForgetPassword";
const Routes = createStackNavigator(
{
Splash: {
screen: Splash,
navigationOptions: {
header: null
}
},
SignUp: {
screen: SignUp,
navigationOptions: () => ({
// header: null
title: "Sign Up",
headerLeft: null,
headerTintColor: "#fc0301",
headerStyle: {
borderBottomColor: "white"
},
headerTitleStyle: {
color: "#fc0301",
textAlign: "center",
flex: 1,
elevation: 0,
fontSize: 25,
justifyContent: "center"
}
})
},
SignIn: {
screen: SignIn,
navigationOptions: {
title: "Sign In",
headerRight: <View />,
headerTintColor: "#fc0301",
headerStyle: {
borderBottomColor: "white"
},
headerTitleStyle: {
color: "#fc0301",
textAlign: "center",
flex: 1,
elevation: 0,
fontSize: 25,
justifyContent: "center"
}
}
},
ForgetPassword: {
screen: ForgetPassword,
navigationOptions: {
header: null
}
},
Home: {
screen: Home,
navigationOptions: {
header: null
}
}
},
{
initialRouteName: "Splash"
}
);
export default createAppContainer(Routes);
推荐答案
您可以检查之前已经关注的屏幕调用警报而不是从导航中检查 routeName
。
You can check for the screen has been focused prior to call the alert instead of checking the routeName
from navigation.
更新后的代码可能如下所示。
the updated code might look like this.
if (this.props.navigation.isFocused()) {
Alert.alert(
"Exit App",
"Do you want to exit?",
[
{
text: "No",
onPress: () => console.log("Cancel Pressed"),
style: "cancel"
},
{ text: "Yes", onPress: () => BackHandler.exitApp() }
],
{ cancelable: false }
);
}
这篇关于如何在一个屏幕中处理退出应用程序而不是整个应用程序 - React Native?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!