React Navigation Android(安卓)返回键事件处理
16lz
2021-01-24
实际开发中有这么一个需求,当停留在首页的时候 点击 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() {// ... }}
更多相关文章
- Ubuntu18.04下载编译Android8.1.0(一)
- android调用系统发送短信 和 已发送短信界面
- 修正android cocos2dx项目当点击属性时提示错误的问题
- 解决Android(安卓)Studio 无法在线更新的问题
- This Android(安卓)SDK requires Android(安卓)Developer Toolki
- android R文件生成错误
- android apk包签名和align
- android 一些小功能实现
- Android实现启动页停留几秒然后自动跳转到App首页的方法