AppState #

AppState can tell you if the app is in the foreground or background, and notify you when the state changes.

AppState is frequently used to determine the intent and proper behavior when handling push notifications.

App States #

  • active - The app is running in the foreground
  • background - The app is running in the background. The user is either in another app or on the home screen
  • inactive - This is a state that occurs when transitioning between foreground & background, and during periods of inactivity such as entering the Multitasking view or in the event of an incoming call

For more information, see Apple's documentation

Basic Usage #

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.

getInitialState: function() { return { currentAppState: AppState.currentState, }; }, componentDidMount: function() { AppState.addEventListener('change', this._handleAppStateChange); }, componentWillUnmount: function() { AppState.removeEventListener('change', this._handleAppStateChange); }, _handleAppStateChange: function(currentAppState) { this.setState({ currentAppState, }); }, render: function() { return ( <Text>Current state is: {this.state.currentAppState}</Text> ); },

This example will only ever appear to say "Current state is: active" because the app is only visible to the user when in the active state, and the null state will happen only momentarily.

Methods #

constructor(0) #

addEventListener(type, handler) #

Add a handler to AppState changes by listening to the change event type and providing the handler

TODO: now that AppState is a subclass of NativeEventEmitter, we could deprecate addEventListener and removeEventListener and just use addListener and listener.remove() directly. That will be a breaking change though, as both the method and event names are different (addListener events are currently required to be globally unique).

removeEventListener(type, handler) #

Remove a handler by passing the change event type and the handler

You can edit the content above on GitHub and send us a pull request!

Examples #

Edit on GitHub
'use strict'; const React = require('react'); const ReactNative = require('react-native'); const { AppState, Text, View } = ReactNative; class AppStateSubscription extends React.Component { state = { appState: AppState.currentState, previousAppStates: [], memoryWarnings: 0, }; componentDidMount() { AppState.addEventListener('change', this._handleAppStateChange); AppState.addEventListener('memoryWarning', this._handleMemoryWarning); } componentWillUnmount() { AppState.removeEventListener('change', this._handleAppStateChange); AppState.removeEventListener('memoryWarning', this._handleMemoryWarning); } _handleMemoryWarning = () => { this.setState({memoryWarnings: this.state.memoryWarnings + 1}); }; _handleAppStateChange = (appState) => { var previousAppStates = this.state.previousAppStates.slice(); previousAppStates.push(this.state.appState); this.setState({ appState, previousAppStates, }); }; render() { if (this.props.showMemoryWarnings) { return ( <View> <Text>{this.state.memoryWarnings}</Text> </View> ); } if (this.props.showCurrentOnly) { return ( <View> <Text>{this.state.appState}</Text> </View> ); } return ( <View> <Text>{JSON.stringify(this.state.previousAppStates)}</Text> </View> ); } } exports.title = 'AppState'; exports.description = 'app background status'; exports.examples = [ { title: 'AppState.currentState', description: 'Can be null on app initialization', render() { return <Text>{AppState.currentState}</Text>; } }, { title: 'Subscribed AppState:', description: 'This changes according to the current state, so you can only ever see it rendered as "active"', render(): React.Element<any> { return <AppStateSubscription showCurrentOnly={true} />; } }, { title: 'Previous states:', render(): React.Element<any> { return <AppStateSubscription showCurrentOnly={false} />; } }, { platform: 'ios', title: 'Memory Warnings', description: 'In the IOS simulator, hit Shift+Command+M to simulate a memory warning.', render(): React.Element<any> { return <AppStateSubscription showMemoryWarnings={true} />; } }, ];