如何在不需要重复的情况下,通过两次单击后退按钮退出应用程序

我一直在寻找一种解决方案,以限制用户,而不是一次点击react native就退出应用程序.

推荐答案

import React, {Component} from 'react';
import {BackHandler, View, Dimensions, Animated, TouchableOpacity, Text} from 'react-native';

let {width, height} = Dimensions.get('window');


export default class App extends Component<Props> {

    state = {
        backClickCount: 0
    };
    
    constructor(props) {
        super(props);

        this.springValue = new Animated.Value(100) ;

    }

    componentWillMount() {
        BackHandler.addEventListener('hardwareBackPress', this.handleBackButton.bind(this));
    }

    componentWillUnmount() {
        BackHandler.removeEventListener('hardwareBackPress', this.handleBackButton.bind(this));
    }

    _spring() {
        this.setState({backClickCount: 1}, () => {
            Animated.sequence([
                Animated.spring(
                    this.springValue,
                    {
                        toValue: -.15 * height,
                        friction: 5,
                        duration: 300,
                        useNativeDriver: true,
                    }
                ),
                Animated.timing(
                    this.springValue,
                    {
                        toValue: 100,
                        duration: 300,
                        useNativeDriver: true,
                    }
                ),

            ]).start(() => {
                this.setState({backClickCount: 0});
            });
        });

    }


    handleBackButton = () => {
        this.state.backClickCount == 1 ? BackHandler.exitApp() : this._spring();

        return true;
    };


    render() {

        return (
            <View style={styles.container}>
                <Text>
                    container box
                </Text>

                <Animated.View style={[styles.animatedView, {transform: [{translateY: this.springValue}]}]}>
                    <Text style={styles.exitTitleText}>press back again to exit the app</Text>

                    <TouchableOpacity
                        activeOpacity={0.9}
                        onPress={() => BackHandler.exitApp()}
                    >
                        <Text style={styles.exitText}>Exit</Text>
                    </TouchableOpacity>

                </Animated.View>
            </View>
        );
    }
}


const styles = {
    container: {
        flex: 1,
        justifyContent: "center",
        alignItems: "center"
    },
    animatedView: {
        width,
        backgroundColor: "#0a5386",
        elevation: 2,
        position: "absolute",
        bottom: 0,
        padding: 10,
        justifyContent: "center",
        alignItems: "center",
        flexDirection: "row",
    },
    exitTitleText: {
        textAlign: "center",
        color: "#ffffff",
        marginRight: 10,
    },
    exitText: {
        color: "#e5933a",
        paddingHorizontal: 10,
        paddingVertical: 3
    }
};

小吃.expo :https://snack.expo.io/HyhD657d7

React-native相关问答推荐

ITMS—91053:缺少API声明—ReactNative

AWS Amplify处理后端错误

如何在使用嵌套的堆栈导航器在选项卡之间导航后弹出到堆栈顶部?

什么是 expoClientId?

在 React Native 中从 Modal 打开 Modal

React-native -run-ios 错误无法构建 iOS 元素.我们运行了xcodebuild命令,但它以错误代码 65 退出

React Native Card Carousel 视图?

使用直接在 Windows 中运行的模拟器在 WSL 中运行 React Native

如何在 React Native 的 Modal 组件上禁用Swipe down to close?

如何在 React Native 中使用断点进行调试

无法加载 exp:// 出了点问题

React Native TextInput blur TouchableHighlight 按下事件

React Native + React (for web) 种子元素

Native Script 与 react native 和 ionic 框架的区别

键盘上方的 React Native 完成按钮

expo 已过期卸载并再次运行以升级

如何在 react native 中将数组保存在异步存储中?

为react-native TextInput 设置边框

react-navigation 标题有一条微弱的线

无法在react-native中分配给# 的只读属性props