我通过以下方式添加了侦听器(尝试同时放入构造函数和 componentDidMount): AppState.addEventListener('change', this._handleAppStateChange);
并在 componentWillUnmount 方法中通过以下方式删除了侦听器:
AppState.removeEventListener('change', this._handleAppStateChange);
并且在回调函数中:
_handleAppStateChange = (nextAppState) => {
setTimeout(() => {
alert('App state: ' + this.state.appState);
alert('Next App state: ' + nextAppState);
}, 0);
}
Run Code Online (Sandbox Code Playgroud)
它会提醒几次。它不会删除一次配置的侦听器。如果有人知道的话请告诉我?