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 问题就解决了