我在BackHandler上有问题,问题是
当运行该应用程序并转到“注册”屏幕并在我的手机上触摸后方时,他们将运行该功能并显示警报以进行确认,但是现在当我转到任何其他屏幕并触摸后方时,我需要返回到每个BackBack.exitApp()上的上一个屏幕;运行,尽管我写的是routname是SignUp,但只是退出应用程序而不是其他屏幕
这是我的代码
注册
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() {....} }
路线
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.
更新的代码可能如下所示.
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 } );}
总结 以上是内存溢出为你收集整理的javascript-如何在一个屏幕而不是整个应用程序中处理退出应用程序-React Native?全部内容,希望文章能够帮你解决javascript-如何在一个屏幕而不是整个应用程序中处理退出应用程序-React Native?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)