Jav*_*ano 15 javascript react-native
是否有可能知道RN应用程序是否已进入后台?任何回调或触发?
如果我听到componentDidUnmount
或componentWillUnmount
正在播放的屏幕,只有当我回到另一个屏幕时才会被触发
谢谢
For*_*ega 20
你可以听听这个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>
);
}
}
Run Code Online (Sandbox Code Playgroud)