检测本机端模式在本机模式中的闭合

问题描述 投票:-1回答:1

如何在react-native中检测本地方模式关闭?

我在我的应用程序中打开了一个本机模式,我希望在关闭后执行某些操作。我使用react-navigation进行导航,但是当本机端模式关闭时,没有任何事件(willFocus等)没有触发。本机模式是使用以下库https://github.com/riwu/react-native-open-notification打开的通知设置。从那里,我使用NotificationSetting.open()函数打开模式。我不知道如何检测用户何时从设置返回到应用程序?尝试检测后退按钮的按下,但是没有运气。

react-native push-notification firebase-notifications
1个回答
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>;
}

}

© www.soinside.com 2019 - 2024. All rights reserved.