安卓物理返回


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 (
            
                
                    返回
                
            
        );
    }
};

export default GoBack;

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

});

你可能感兴趣的:(安卓物理返回)