componentDidMount () {
BackHandler.addEventListener('hardwareBackPress', this.onBackAndroid);
}
componentWillUnmount () {
BackHandler.removeEventListener('hardwareBackPress', this.onBackAndroid);
}
onBackAndroid = () => {
if (this.props.navigation.isFocused()) {
if (this.lastBackPressed && this.lastBackPressed + 2000 >= Date.now()) {
//最近2秒内按过back键,可以退出应用。
BackHandler.exitApp();//直接退出APP
} else {
this.lastBackPressed = Date.now();
ToastUtil.show('再次点击退出App')
}
return true;
}
}
(笔记)react native 双击退出app
最新推荐文章于 2023-11-27 10:21:42 发布