【问题标题】:react native. the close event of the application反应原生。应用程序的关闭事件
【发布时间】:2018-12-13 09:52:25
【问题描述】:

在为 ios on react native 开发的应用程序的一个页面上,有一个开关组件。与它们交互时,开关的状态存储在 Redux 存储中,但在离开页面或离开应用程序时,需要将状态数据发送到服务器。离开页面实现如下:

props.navigation.addListener('willBlur', async() => {
  props.pushDataOnServer(data, token);
});

我们为willBlur 事件添加了一个导航处理程序(StackNavigator),因此我们可以捕捉到另一个页面的转换,但是当直接从开关的编辑页面关闭应用程序时呢?是否有任何事件(例如willExitwillClose)?或者如果您知道更有效的方法,请告诉我们

【问题讨论】:

    标签: javascript ios reactjs react-native redux


    【解决方案1】:

    在您的根组件中,您可以观察 AppState 的变化。

    componentDidMount() {
      AppState.addEventListener('change', this.handleAppStateChange);
    }
    
    componentWillUnmount() {
      AppState.removeEventListener('change', this.handleAppStateChange);
    }
    
    handleAppStateChange = (nextAppState) => {
      if (nextAppState === 'inactive') {
        console.log('the app is closed');
      }    
    }
    

    【讨论】:

    • 我想在安装应用程序时获取状态,但既不是在后台也不是在前台。我能得到任何帮助吗?
    • isactive 用于应用在后台未关闭时
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-05
    相关资源
    最近更新 更多