在 react-native 中检测本机端模态关闭
Detect native side modal closing in react-native
如何在 react-native 中检测本机端模式关闭?
我在我的应用程序中打开了一个本机模式,我希望在它关闭后执行某个操作。我使用 react-navigation 进行导航,但是当本机端模式关闭时,它的 none 事件(willFocus 等)没有触发。本机端模式是使用此库打开的通知设置:https://github.com/riwu/react-native-open-notification。从那里我打开应用 NotificationSetting.open() 函数的模式。我不知道如何检测用户 returns 何时从设置返回应用程序?尝试检测后退按钮按下,但没有成功。
认为我可以使用 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>;
}
}
如何在 react-native 中检测本机端模式关闭?
我在我的应用程序中打开了一个本机模式,我希望在它关闭后执行某个操作。我使用 react-navigation 进行导航,但是当本机端模式关闭时,它的 none 事件(willFocus 等)没有触发。本机端模式是使用此库打开的通知设置:https://github.com/riwu/react-native-open-notification。从那里我打开应用 NotificationSetting.open() 函数的模式。我不知道如何检测用户 returns 何时从设置返回应用程序?尝试检测后退按钮按下,但没有成功。
认为我可以使用 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>;
}
}