如何在react-native中检测本地方模式关闭?
我在应用程序中打开了本机模式,并且希望在关闭后执行某些操作。我使用react-navigation进行导航,但是当本地方模式关闭时,没有任何事件(willFocus等)没有触发。本机模式是通过使用以下库打开的通知设置:https://github.com/riwu/react-native-open-notification。从那里,我使用NotificationSetting.open()函数打开模式。我不知道如何检测用户何时从设置返回到应用程序?尝试检测后退按钮的按下,但是没有运气。
答案 0 :(得分:0)
想通了我可以使用react-native的AppState(https://facebook.github.io/react-native/docs/appstate):
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>;
}
}