React-Native 的BackAndroid

BackHandler or BackAndroid

BackAndroid 已经废弃

这个只有三个static 方法

    static exitApp() 
    static addEventListener(eventName, handler) 
    static removeEventListener(eventName, handler)

使用说明

Navigator.initialRoute配置的Component

因为在Navigator配置页面中 this.props.navigator is undefined is not an object 不知道为啥这样????
解决方法

在第一个 页面 中设置 如下

    _onBackEvent() {
        const navigator = this.props.navigator;
        let count = navigator.getCurrentRoutes();
        if (count.length > 1) {
            navigator.pop();
            return true;
        } else {
            let timestamp = (new Date()).valueOf();
            if (timestamp - firstClick > 2000) {
                firstClick = timestamp;
                ToastAndroid.show('再按一次退出', ToastAndroid.SHORT);
                return true;
            }
        }
        return false;
    }
    <!--添加到constructorthis.onBackEvent=this._onBackEvent.bind(this)-->
    componentWillMount() {
        BackHandler.addEventListener("hardwareBackPress", this.onBackEvent);
    }
    componentWillUnmount() {
        BackHandler.removeEventListener("hardwareBackPress", this.onBackEvent);
    }

<span id="jump">解决不能在<Navigator Component />中设置返回和 this.props.navigator is undefined is not an object 错误

</span>

     <Navigaor  ref={component => this.navigator = component}
        ...
     />
     <!--加入 ref={component => this.navigator = component}-->

在_onBackEvent 使用 的this.props.navigator 换成 this.navigator 问题就解决了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值