AppState API tells you if the app is in the foreground or background and notifies you when the state changes. This is useful for determining whether to pause/resume activities, save data, or adjust behavior based on app visibility.
Import
Properties
currentState
Returns the current state of the application.AppStateStatus - One of: 'active', 'background', 'inactive', 'extension', or 'unknown'
isAvailable
Indicates whether the AppState API is available.boolean
Methods
addEventListener()
Add a listener to monitor app state changes.string
required
The event type to listen for:
'change': App state has changed (active, background, inactive)'memoryWarning': Device is running low on memory'focus': App has gained focus (Android only)'blur': App has lost focus (Android only)
function
required
Callback function invoked when the event occurs.For
'change' events, receives the new AppStateStatus as parameter.
For other events, receives no parameters.EventSubscription - Object with a remove() method to unsubscribe.
App States
active
The app is running in the foreground and the user is actively interacting with it.background
The app is running in the background. The user is either:- In another app
- On the home screen
- On another Activity (Android)
inactive (iOS only)
Transitional state that occurs when moving between foreground and background, or during:- Entering the multitasking view
- Opening the Notification Center
- Receiving an incoming call
- Opening Control Center
extension
The app is running as an app extension (rare).unknown
The app state is not yet determined.Examples
Basic Usage
Handling Active/Inactive Transitions
Pausing/Resuming Activities
Memory Warning Handler
Focus/Blur Events (Android)
Tracking Time in Foreground
Platform Differences
iOS
- Supports
'active','background', and'inactive'states - The
'inactive'state is used during transitions and system interactions - Background execution time is limited by iOS
Android
- Primarily uses
'active'and'background'states - The
'inactive'state is less common - Additional
'focus'and'blur'events for more granular control - Background execution policies vary by Android version
Best Practices
On Android, the app may be killed when in the background. Save important data before the app goes to the background.
Common Use Cases
- Auto-pause media: Pause audio/video when app goes to background
- Save data: Persist user data before app is backgrounded
- Refresh content: Reload data when app returns to foreground
- Analytics: Track session duration and user engagement
- Resource management: Release heavy resources when app is not visible
- Real-time updates: Pause/resume subscriptions based on app state