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.
To see the current state, you can check AppState. currentState , which will be kept up-to-date. However, currentState will be null at launch while AppState retrieves it over the bridge.
What is AppState in React Native? In React Native, AppState represents the current state of the app — i.e., whether the app is in the foreground or background. AppState is useful for collecting data on app usage — for example, the time a user spends in the app before putting it in the background or closing the app.
With @react-native-community/hooks you can use the useAppState hook to check the app state. When you close the app, it gets a state of unknown once you open it back. When is in background, it says background' or 'inactive'.
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> ); } }
source: https://facebook.github.io/react-native/docs/appstate.html
you can use AppState
read this document
https://facebook.github.io/react-native/docs/appstate.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With