ReactNativeUtil icon indicating copy to clipboard operation
ReactNativeUtil copied to clipboard

android 返回键

Open wuyunqiang opened this issue 7 years ago • 0 comments

   onBackAndroid = () => {
        // 最近2秒内按过back键,可以退出应用。
        // if (this.lastBackPressed && this.lastBackPressed + 2000 >= Date.now()) {
        //     return false;
        // }
        // this.lastBackPressed = Date.now();
        const nav = this.props.nav;//获取redux的navigation state
        console.log('nav', nav);
        const routes = nav.routes;
        if (routes.length > 1) {
            this.props.navigation.goBack(null);
            return true;
        }else if(routes[0].routes[routes[0].index].routeName!=routes[0].routes[0].routeName){//当前页面不等于第一个页面 跳转至第一个页面
            this.props.navigation.navigate(routes[0].routes[0].routeName);
            return true;
        }
        return false;
    };

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

    }
    componentDidMount() {
        console.log('navigation',this.props.navigation);
        if(Platform.OS==='android'){
            BackHandler.addEventListener('hardwareBackPress', this.onBackAndroid);
        }
    }

wuyunqiang avatar Nov 20 '17 02:11 wuyunqiang