React Native 监听Back双击退出实现+APP前后台状态监听
2018-01-16 14:14
519 查看
/**
* 关于前后台监听,HOME直接后台不执行component相关周期函数,back
* 后台是执行的,back后返回应该是重新render了,所以监听HOME或者
* BACK可以同一使用APPState的状态码来操作
* APPState是有三个状态码的,有一个不常用就没写
*/
* 关于前后台监听,HOME直接后台不执行component相关周期函数,back
* 后台是执行的,back后返回应该是重新render了,所以监听HOME或者
* BACK可以同一使用APPState的状态码来操作
* APPState是有三个状态码的,有一个不常用就没写
*/
import React, { Component } from 'react'; import { ToastAndroid, AppState, BackHandler, TouchableHighlight, Platform, StyleSheet, Text, View, Alert } from 'react-native'; const instructions = Platform.select({ ios: 'Press Cmd+R to reload,\n' + 'Cmd+D or shake for dev menu', android: 'Double tap R on your keyboard to reload,\n' + 'Shake or press menu button for dev menu', }); const lastBackPressed = Date.now(); type State = { test: string, }; export default class App extends Component<Props,State>{ constructor(props: Props){ super(props); this.state = { test: "hello", }; } //组件加载之后添加监听 componentDidMount() { if(Platform.OS === 'android') BackHandler.addEventListener('hardwareBackPress', this._onBackPressed); AppState.addEventListener('change', this._onAppStateChanged); } //组件卸载之前移除监听 componentWillUnmount() { if(Platform.OS === 'android') BackHandler.removeEventListener('hardwareBackPress', this._onBackPressed); AppState.removeEventListener('change', this._onAppStateChanged); } _onBackPressed() { if (lastBackPressed && lastBackPressed + 2000 >= Date.now()) { BackHandler.exitApp(); } lastBackPressed = Date.now(); ToastAndroid.show('再按一次退出应用', ToastAndroid.SHORT); return true; } _onAppStateChanged() { switch (AppState.currentState) { case "active": console.log("active"); break; case "background": console.log("background"); break; default: } } render() { return ( <View style={styles.container}> <Text style={styles.welcome}> Welcome to React Native! </Text> <Text style={styles.instructions}> To get started, edit App.js </Text> <TouchableHighlight onPress={ ()=> { this.setState({test: "aaaa"}); } }> <Text>{this.state.test}</Text> </TouchableHighlight> <Text style={styles.instructions}> {instructions} </Text> </View> ); } } const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#F5FCFF', }, welcome: { fontSize: 20, textAlign: 'center', margin: 10, }, instructions: { textAlign: 'center', color: '#333333', marginBottom: 5, }, });
相关文章推荐
- Android 监听app前台、后台状态切换的实现方案
- Android中如何实现双击退出APP
- android安卓如何实现单击提示“再按一次退出”,双击退出,实现双击监听
- android 实现双击back键退出程序snackbar提示
- Android 双击Back键退出应用的实现方法
- android安卓如何实现单击提示“再按一次退出”,双击退出,实现双击监听
- Android简单实现连点两次Back键退出App
- 自定义基类activity实现双击back键退出应用
- MUI 监听返回(mui.back)按2次返回退出app
- 防止用户误操作返回键导致程序退出,监听back键的状态做统一处理
- iOS 监听APP被强制退出的方法,双击home杀死应用
- Android实现按下Back键程序隐藏到后台,而不是直接退出
- [置顶] 利用ActivityLifecycleCallBack监控app前后台状态切换,实现手势密码即九宫格解锁
- 安卓:解决应用前后台切换报The surface has been released错误;PhoneStateListener实现通话状态的监听
- 解决app 监听应用退出 需要数据还原问题(例如账户登陆状态的保持)
- App实现双击退出程序
- Android几行代码实现双击back键退出程序
- React Native(七):Android双击Back键退出应用
- Android按返回键程序仍在后台运行,并未完全退出和销毁,类似微信QQ等退出方式的实现-- moveTaskToBack
- 双击Back退出实现