How to detect when a React Native app is opened?

I fixed this by using the AppStateIOS API instead of AppState. The later works as expected on iOS devices: when the app goes to the background the "change" event fires, and again when it comes back to the foreground. The AppState API doesn’t seem to fire the "change" event at all on an iOS device, as of React Native v0.18, as far as I can tell. The project’s conventions suggest that AppState should be a cross-platform wrapper around AppStateIOS, but that doesn’t seem to be the case here.

The following example should demonstrate the issue:

React.createClass({

  componentDidMount() {
    AppStateIOS.addEventListener('change', state =>
      console.log('AppStateIOS changed to', state)
    )

    AppState.addEventListener('change', state =>
      console.log('AppState changed to', state)
    )
  },

  render() {
    return <View/>
  }

})

When this component is mounted into a React Native app, you will see “AppStateIOS changed to …” when closing and opening the app. You will never see “AppState changed to …” as far as I’m aware.

Update

It appears that this was fixed in React Native recently (as of v26). You can now use AppState as advertised on iOS and Android.

Leave a Comment

tech