react native两次点击返回按钮退出APP


官网介绍: https://reactnative.cn/docs/0.44/backhandler.html

使用前引入

improt {Platform,BackHandler} from react native

componentDidMount() {

if (Platform.OS === 'android'){
  BackHandler.addEventListener('hardwareBackPress', this.onBackAndroid);
}
}

componentWillUnmount() {

if (Platform.OS === 'android') {
  BackHandler.removeEventListener('hardwareBackPress', this.onBackAndroid);
}
}

onBackAndroid = () => {

//禁用返回键
if(this.props.navigation.isFocused()) {//判断   该页面是否处于聚焦状态
    if (this.lastBackPressed && this.lastBackPressed + 2000 >= Date.now()) {
      //最近2秒内按过back键,可以退出应用。
      // return false;
      BackHandler.exitApp();//直接退出APP
    }else{
      this.lastBackPressed = Date.now();
      ToastAndroid.show('再按一次退出应用', 1000);//提示
      return true;
    }
}
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM