react-native-router-flux物理返回键

1.引入BackHandler 从react-native中

2.在componentDidMount中添加下面那行监听代码

this.backHandler = BackHandler.addEventListener('hardwareBackPress', this.handleBackPress);

3.在componentWillUnmount添加

componentWillUnmount() {
this.backHandler.remove()
}

4.在handleBackPress函数中写相关返回逻辑

handleBackPress = () => {
Actions.pop(); // 跳转,可以跳转到想去的页面或位置,根据react-native-router-flux组件的方法
console.log("wo handle back press detail")
// //this.goBack(); // works best when the goBack is async
return true;
}

 如果想要点2次退出应用:可以这么写

handleBackPress = () => {
if (this.lastBackPressed && this.lastBackPressed + 2000 >= Date.now()) {
//最近2秒内按过back键,可以退出应用。
BackHandler.exitApp();//退出整个应用
return false
}

this.lastBackPressed = Date.now();
console.log('再按一次要退出了')
ToastAndroid.show('再按一次退出应用', ToastAndroid.SHORT);

return true;
}

 官网:

https://facebook.github.io/react-native/docs/backhandler.html



转载于:https://www.cnblogs.com/lude1994/p/11185286.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值