我如何从React-Native获取onresume事件? [英] How do I get onresume event from React-Native?

查看:529
本文介绍了我如何从React-Native获取onresume事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从React-Native获取onresume事件?我想在恢复Android主要活动时进行一些检查.我检查了RN的源代码,但恢复应用程序时似乎没有任何事件.

How do I get onresume event from React-Native? I want to do some check when Android main activity resumed. I check source code of RN, but it seems no event when app resumed.

推荐答案

React Native提供了 AppState 来告知应用程序处于前台还是后台,并在状态更改时通知我们.

React Native provides AppState to tell if the app is in the foreground or background, and notify us when the state changes.

    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>
        );
      }

    }

来源: https://facebook.github.io/react-native/docs/appstate.html

这篇关于我如何从React-Native获取onresume事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆