1
是否有可能知道一个RN应用程序是否已经背景?任何回调或触发器?如何知道反应本机应用程序是否转到后台?
如果我听componentDidUnmount
或我在屏幕componentWillUnmount
,它只会如果我去前/后到另一个屏幕
感谢
是否有可能知道一个RN应用程序是否已经背景?任何回调或触发器?如何知道反应本机应用程序是否转到后台?
如果我听componentDidUnmount
或我在屏幕componentWillUnmount
,它只会如果我去前/后到另一个屏幕
感谢
你可以听appState
触发的事件。 从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>
);
}
}
可以使用AppState
:
应用美国
active
- 应用程序在运行 - 应用程序在前台background
运行的背景。用户无论是在另一个应用程序或主屏幕- 的事件
inactive
上 - 这是前景&背景之间转变时出现的状态,并且在非活动期间,如在进入多任务视图或呼入
的可能的复制[本地做出反应?有没有当你的应用程序被关闭回调函数(https://stackoverflow.com/questions/38677137/react-native-is-there-a-callback功能全换时,你的应用程序内,是封闭的) – bennygenel