Javascript 如何在react native上检测kill应用程序中的应用程序?
我想在应用程序被用户终止时更改API中的状态数据 我尝试过在应用程序关闭时使用componentWillUnmount来更改数据,我还使用AppStateJavascript 如何在react native上检测kill应用程序中的应用程序?,javascript,android,react-native,Javascript,Android,React Native,我想在应用程序被用户终止时更改API中的状态数据 我尝试过在应用程序关闭时使用componentWillUnmount来更改数据,我还使用AppState _handleAppStateChange = (nextAppState) => { if ( this.state.appState.match(/inactive|background/) && nextAppState === 'active' ) { co
_handleAppStateChange = (nextAppState) => {
if (
this.state.appState.match(/inactive|background/) &&
nextAppState === 'active'
) {
console.log('App has come to the foreground!')
}
this.setState({ appState: nextAppState })
}
我希望当应用程序被用户终止时,它可以自动更改状态。尝试以下操作:
componentDidMount() {
AppState.addEventListener('change',
this.handleAppStateChange);
}
componentWillUnmount() {
AppState.removeEventListener('change', this.handleAppStateChange);
}
handleAppStateChange = (nextAppState) => {
if (nextAppState === 'inactive') {
console.log('the app is closed');
}
}
nextapstate==='inactive'
或检查background
是否不会告诉您应用程序是否被“杀死”(刷出)
因为当本机层被终止时,javascript引擎也被终止。因此,没有办法告诉javascript应用程序已被杀死
如果您只想在应用程序被终止后初始化某些内容,您可以在基本组件的构造函数
或componentDidMount
中启动一些函数,您知道该基本组件只会为您的应用程序安装一次
补充:现在我们可以(可能更喜欢)使用useffect(()=>{},[])
代替上面的例子
如果你真的想确保应用程序在之前被终止,也许你可以将
nextapstate
存储到某个持久化存储中,并检查该值是否匹配inactive或background,因为应用程序状态在应用程序初始启动时不会更改。它在IOS上运行,但不在Android上运行。你对安卓有什么想法吗?我查过了,安卓和ios都能用already@user3751548我也有同样的问题,有什么解决办法吗?请帮助检查哪些状态可用于android和ios