gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 23:14:47 33 4
gpt4 key购买 nike

如何从 React-Native 获取 onresume 事件?我想检查一下 Android 主要 Activity 何时恢复。我检查了RN的源代码,但是应用程序恢复时似乎没有任何事件。

最佳答案

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

}

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

关于android - 如何从 React-Native 获取 onresume 事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33643359/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com