实际开发中有这么一个需求,当停留在首页的时候 点击 Android的返回键,提示再次点击退出,直接上代码

  import React from "react";  import { BackHandler } from "react-native";  class ScreenWithCustomBackBehavior extends React.Component {    _didFocusSubscription;    _willBlurSubscription;    constructor(props) {      super(props);    this._didFocusSubscription = props.navigation.addListener('didFocus', payload =>      BackHandler.addEventListener('hardwareBackPress', this.onBackButtonPressAndroid)  ) ;}componentDidMount() {    this._willBlurSubscription = this.props.navigation.addListener('willBlur', payload =>     BackHandler.removeEventListener('hardwareBackPress', this.onBackButtonPressAndroid)  );}onBackButtonPressAndroid = () => {    if (判断条件) {    //需要操作的事件(如:再次点击退出应用)  return true;    } else {  return false;  }} ;componentWillUnmount() {  this._didFocusSubscription && this._didFocusSubscription.remove();  this._willBlurSubscription && this._willBlurSubscription.remove();}render() {// ...  }}

更多相关文章

  1. Ubuntu18.04下载编译Android8.1.0(一)
  2. android调用系统发送短信 和 已发送短信界面
  3. 修正android cocos2dx项目当点击属性时提示错误的问题
  4. 解决Android(安卓)Studio 无法在线更新的问题
  5. This Android(安卓)SDK requires Android(安卓)Developer Toolki
  6. android R文件生成错误
  7. android apk包签名和align
  8. android 一些小功能实现
  9. Android实现启动页停留几秒然后自动跳转到App首页的方法

随机推荐

  1. Android实用小技巧(持续更新)
  2. 对比Appium和Calabash
  3. Android导入工程提示Invalid project des
  4. Android(安卓)Room 增删改查 个人经验
  5. Android(安卓)webkit 事件传递流程通道分
  6. android 分支 详解
  7. 动态添加权限
  8. android中的 touch事件
  9. Android(安卓)RecyclerView DiffUtil 局
  10. 2017 年你应该了解的Android(安卓)库