An Activity represents a single screen with a user interface. It is the most visible and fundamental component of the Android Application Framework.
However, an Activity is not just a UI class; it is a complex state machine managed remotely by the operating system.
The Activity Lifecycle
The lifecycle methods (onCreate, onStart, onResume, onPause, onStop, onDestroy) are not called randomly. They are strictly orchestrated by the ActivityManagerService (AMS) inside the System Server.
When the user presses the Home button, your app does not decide to pause itself.
- The AMS detects the Home button press.
- The AMS sends a Binder IPC message to your app's
ActivityThread. - The
ActivityThreadexecutes youronPause()andonStop()callbacks.
If you block the main thread during onPause() with a heavy database query, you freeze the entire AMS transition, leading to a sluggish user experience and potentially an ANR (Application Not Responding) crash.
Launch Modes
Android manages Activities in a "Task Stack" (a Last-In-First-Out stack). You can violently alter how an Activity is placed into this stack using Launch Modes defined in the AndroidManifest.xml (or via Intent flags).
standard: The default. A new instance of the Activity is created every single time it is launched, even if it is already on the top of the stack.singleTop: If an instance of the Activity is already at the absolute top of the stack, Android routes the new Intent toonNewIntent()instead of creating a new instance.singleTask: The Activity sits at the root of a new task. If it already exists in a task, all Activities above it are destroyed (popped), and it receivesonNewIntent().singleInstance: Similar tosingleTask, but the Activity is the only member of its task. No other Activities can be launched into that task.
<!-- Example of setting a launch mode in the manifest -->
<activity
android:name=".MainActivity"
android:launchMode="singleTop"
android:exported="true">
</activity>
ActivityRecord in AMS
To the Java app developer, an Activity is a subclass containing UI logic.
To the platform engineer working in the System Server, an Activity is represented as an ActivityRecord.
Inside the AMS, the ActivityRecord object holds all the crucial metadata required to manage the screen: its specific launch mode, its current lifecycle state, its memory footprint, and the Binder token used to communicate with the actual app process.
# Platform engineers use dumpsys to view the raw ActivityRecord states inside AMS
adb shell dumpsys activity activities