如何从React-Native获取onresume事件?

use*_*039 21 android react-native

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

小智 5

React Native提供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>
        );
      }

    }
Run Code Online (Sandbox Code Playgroud)

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


Kes*_*era 5

每次用户返回此屏幕时都会调用此方法。

此方法与Android方法相同onResume()

 render() {
 this.props.navigation.addListener(
    'didFocus',
       payload => {
         console.log("Payload is called .....................")
        }
  );
}
Run Code Online (Sandbox Code Playgroud)