Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get onresume event from React-Native?

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.

like image 261
user5549039 Avatar asked Nov 11 '15 02:11

user5549039


People also ask

How do you get current state in react native?

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 API in react native?

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.

How do you know app is closed in react native?

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'.


2 Answers

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

like image 101
jayad aadrit Avatar answered Sep 20 '22 01:09

jayad aadrit


you can use AppState
read this document
https://facebook.github.io/react-native/docs/appstate.html

like image 28
pooria Avatar answered Sep 22 '22 01:09

pooria