Foreground and Background States
Foreground and Background States
1. Application process overview
When an Android application starts, Android creates a Linux process for it.
flowchart TD
A["User opens app"] --> B["Application process is created"]
B --> C["Application.onCreate() runs"]
C --> D["Activity is created"]
D --> E["App becomes visible"]
The process can remain alive even when the app is no longer visible.
2. Foreground state
An app is in the foreground when the user can see and interact with one of its Activities.
Typical lifecycle:
flowchart TD
A["Activity.onCreate()"] --> B["Activity.onStart()"]
B --> C["Activity.onResume()"]
C --> D["User interacts with the app"]
Examples:
- User is scrolling a screen
- User is typing in a form
- Camera screen is open
- Video is playing on the screen
When an Activity is resumed, its process normally receives the highest importance.
3. Partially visible state
Sometimes an Activity is visible but cannot receive user interaction.
Example:
flowchart TD
A["Activity is visible"] --> B["Transparent dialog opens"]
B --> C["Activity.onPause() runs"]
The Activity remains visible behind the dialog, but it is not fully interactive.
The application process is normally considered a visible process.
4. Background state
The app enters the background when none of its Activities are visible.
Common situations:
- User presses the Home button
- User opens another application
- Screen is locked
- User switches apps using the Recent Apps screen
Typical lifecycle:
flowchart TD
A["Activity.onPause()"] --> B["Activity.onStop()"]
B --> C["App enters the background"]
Important:
Background state does not mean process death. The application process may still be alive in memory.
These objects may still exist:
- Application object
- ViewModels
- Singleton objects
- Static variables
- Repository objects
- In-memory caches
However, Android can kill the process later when memory is required.
5. Detecting foreground and background
Use ProcessLifecycleOwner to observe the lifecycle of the complete application process.
class MyApplication :
Application(),
DefaultLifecycleObserver {
override fun onCreate() {
super.onCreate()
ProcessLifecycleOwner
.get()
.lifecycle
.addObserver(this)
}
override fun onStart(owner: LifecycleOwner) {
Log.d("AppState", "App entered foreground")
}
override fun onStop(owner: LifecycleOwner) {
Log.d("AppState", "App entered background")
}
}Register the custom Application class:
<application
android:name=".MyApplication"
android:theme="@style/Theme.MyApp">
</application>6. `Application.onCreate()` versus app foreground
These are different events.
flowchart TD
A["New application process starts"] --> B["Application.onCreate() is called"]flowchart TD
A["App enters the foreground"] --> B["ProcessLifecycleOwner.onStart() is called"]Example:
flowchart TD
A["App process starts"] --> B["Application.onCreate()"]
B --> C["App enters the foreground"]
C --> D["ProcessLifecycleOwner.onStart()"]
D --> E["User presses Home"]
E --> F["ProcessLifecycleOwner.onStop()"]
F --> G["User returns to the app"]
G --> H["ProcessLifecycleOwner.onStart()"]
Application.onCreate() is not called again because the same process is still alive.
7. Quick comparison
| State | Activity condition | User interaction |
|---|---|---|
| Foreground | Resumed | Yes |
| Partially visible | Paused but visible | Limited or no |
| Background | Stopped | No |
| Process killed | Process does not exist | No |
Interview answer
The app is in the foreground when a visible Activity is resumed and the user can interact with it. It moves to the background when all Activities are stopped. Moving to the background does not immediately destroy the app process. Android may keep the process in memory as a cached process so the application can reopen quickly. ProcessLifecycleOwner can be used to observe application-level foreground and background transitions.