我到处都找过,但找不到这个问题的答案。我如何检测用户何时尝试关闭我的 React Native 应用程序(如进程正在运行,并且他们手动管理其应用程序并强制退出它)。我想在发生这种情况时添加注销功能,但是找不到检测它的方法。AppState
似乎只检测应用程序何时进入和退出后台。
看起来您可以检测之前的状态并将其与下一个状态进行比较。从我在网上找到的信息来看,您无法检测到应用程序正在关闭还是进入后台,但您可以检测到它是否已关闭inactive
(已关闭),或在background
.
示例来自反应本机文档 https://facebook.github.io/react-native/docs/appstate.html
import React, {Component} from 'react'
import {AppState, Text} from 'react-native'
class AppStateExample extends Component {
state = {
appState: AppState.currentState
}
componentDidMount() {
AppState.addEventListener('change', this._handleAppStateChange);
}
componentWillUnmount() {
AppState.removeEventListener('change', this._handleAppStateChange);
}
_handleAppStateChange = (nextAppState) => {
if (this.state.appState.match(/inactive|background/) && nextAppState === 'active') {
console.log('App has come to the foreground!')
}
this.setState({appState: nextAppState});
}
render() {
return (
<Text>Current state is: {this.state.appState}</Text>
);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)