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;
   }
   <!--添加到constructor 中this.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 问题就解决了

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容