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 foregroundbackground
- The app is running in the background. The user is either:- in another app
- on the home screen
- [Android] on another
Activity
(even if it was launched by your app)
- [iOS]
inactive
- This is a state that occurs when transitioning between foreground & background, and during periods of inactivity such as entering the multitasking view, opening the Notification Center 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.
- Function Component
- Class Component
If you don't want to see the AppState update from active
to inactive
on iOS you can remove the state variable and use the appState.current
value.
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. If you want to experiment with the code we recommend to use your own device instead of embedded preview.
Reference
Eventsβ
change
β
This event is received when the app state has changed. The listener is called with one of the current app state values.
memoryWarning
β
This event is used in the need of throwing memory warning or releasing it.
focus
Androidβ
Received when the app gains focus (the user is interacting with the app).
blur
Androidβ
Received when the user is not actively interacting with the app. Useful in situations when the user pulls down the notification drawer. AppState
won't change but the blur
event will get fired.
Methodsβ
addEventListener()
β
addEventListener(eventType, listener);
Sets up a function that will be called whenever the specified event type on AppState occurs. Valid values for eventType
are
listed above. Returns the EventSubscription
.
removeEventListener()
β
removeEventListener(eventType, listener);
Deprecated. Use the
remove()
method on theEventSubscription
returned byaddEventListener()
.
Propertiesβ
currentState
β
AppState.currentState;