1.在componentWillMount中
componentWillMount() {
this.backHandler && this.backHandler.remove();
}2.在componentDidMount中
componentDidMount() {
this.backHandler = BackHandler.addEventListener('hardwareBackPress',
this.onBackButtonPressAndroid);
}3.退出函数
onBackButtonPressAndroid = () => {
if (this.props.navigation.isFocused()) {
if (this.lastBackPressed && this.lastBackPressed + 2000 >= Date.now()) {
//最近2秒内按过back键,可以退出应用。
return false;
}
this.lastBackPressed = Date.now();
NRJCore.toast("再按一次退出");
return true;
}
}