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

Android 如何从React Native获取onresume事件?,android,react-native,Android,React Native,如何从React Native获取onresume事件? 我想在Android主活动恢复时做一些检查。 我检查了RN的源代码,但应用程序恢复时似乎没有任何事件。您可以使用AppState 阅读此文档 React Native提供了AppState来判断应用程序是在前台还是后台,并在状态更改时通知我们 import React, {Component} from 'react' import {AppState, Text} from 'react-native' cla

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

您可以使用AppState
阅读此文档

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

    }
import React,{Component}来自“React”
从“react native”导入{AppState,Text}
类AppStateExample扩展组件{
状态={
appState:appState.currentState
}
componentDidMount(){
AppState.addEventListener('change',this.\u handleAppStateChange);
}
组件将卸载(){
AppState.removeEventListener('change',this.\u handleAppStateChange);
}
_HandLeapStateChange=(nextAppState)=>{
if(this.state.appState.match(/inactive | background/)和&nextapstate=='active'){
console.log('应用程序已到达前台!')
}
this.setState({appState:nextapstate});
}
render(){
返回(
当前状态为:{this.state.appState}
);
}
}

来源:

我最终推出了自己的解决方案。它的实现非常简单

确保所有活动都扩展到同一个类,并在其中执行以下操作:

import com.facebook.react.bridge.Arguments
import com.facebook.react.modules.core.DeviceEventManagerModule

override fun onResume() {
    super.onResume()

    val params = Arguments.createMap()
    params.putInt("rootViewTag", reactRootView!!.rootViewTag)
    val reactContext = reactRootView!!.reactInstanceManager!!.currentReactContext!!
    reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java).emit("androidActivityResumed", params)
}
然后,在反应方面执行以下操作:

import {DeviceEventEmitter} from 'react-native'

class MyComp extends React.Component {
  componentDidMount() {
    DeviceEventEmitter.addListener('androidActivityResumed', this._onAndroidActivityResumed)
  }

  componentWillUnmount() {
    DeviceEventEmitter.removeListener('androidActivityResumed', this._onAndroidActivityResumed)
  }

  _onAndroidActivityResumed = (e) => {
    if (e.rootViewTag === this.props.rootTag) {
      // Do whatever you want here
    }
  }
}

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

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

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

请提供更多信息我想做的请求时,使用返回到我的应用程序时,用户按下主页按钮。准确回答this@PranavPrakash我是说安卓系统。但最近Facebook刚刚为AppState打开了新的API源代码。我想知道“活动”的状态,而不是整个应用程序的状态。类似于onResume()Android方法。我该怎么做?如何在其他函数中使用它而不是render()?太好了!这应该是选定的答案
 render() {
 this.props.navigation.addListener(
    'didFocus',
       payload => {
         console.log("Payload is called .....................")
        }
  );
}