安卓物理返回

2019-10-23  本文已影响0人  知命者夏洛特

import React from 'react';
import { StyleSheet, Text, View, Platform,Alert,  BackHandler, SafeAreaView } from 'react-native';

class GoBack extends React.Component {
    static navigationOptions = () => {
        return {
            headerLeft: null,
            header: null,
            gesturesEnabled: false //ios禁止返回
        }
    }

    constructor(props) {
        super(props);
        this.state = {
            
        };

    }

    componentDidMount() {
        if (Platform.OS === 'android') {
            BackHandler.addEventListener('hardwareBackPress', this._backHandler);
        }
    }

    //返回时执行,右键
    _backHandler = () => {
        Alert.alert('安卓物理返回')
    }

    componentWillUnmount() {
        if (Platform.OS === 'android') {
            BackHandler.removeEventListener('hardwareBackPress', this._backHandler);
        }
    }

    render() {
        return (
            <View style={styles.container_view}>
                <SafeAreaView>
                    <Text>返回</Text>
                </SafeAreaView>
            </View>
        );
    }
};

export default GoBack;

const styles = StyleSheet.create({
    container_view: {
        flex: 1,
        backgroundColor:'#fff'
    },

});

上一篇下一篇

猜你喜欢

热点阅读