
Introduction to Android Activity
An Activity is one of the core components of Android development. It acts as a container for a user interface and handles user interaction. Think of each activity as a single screen in an app: the login screen, dashboard, settings, or a messaging interface. Activities enable developers to design user flows and manage state transitions throughout the app lifecycle.
Each Activity extends the android.app.Activity
or more commonly androidx.appcompat.app.AppCompatActivity
class, making it part of the app’s visible behavior. When a user launches an app, an activity is usually the entry point.
Activities are also essential for interacting with other apps, sharing data, and managing multi-tasking operations. Android’s unique component architecture allows activities to be dynamically loaded, paused, and resumed, enhancing performance and resource management.
Major Use Cases of Android Activity
2.1. UI Presentation and Interaction
Each activity hosts a screen layout defined in XML, allowing for a rich user interface with buttons, text fields, lists, and other components. Activities capture user input, validate it, and update the UI accordingly.
2.2. Navigation and Flow Control
Activities are pivotal in determining how users navigate between screens. Using Intents, you can start other activities within the app or even invoke components in other apps.
2.3. Lifecycle-Driven Resource Management
Activities offer fine-grained control over app behavior as the device state changes. For example, you can pause animations, save unsaved changes, or release hardware resources when an activity is paused or stopped.
2.4. Data Communication Between Components
With Intent
objects and Bundle
extras, you can pass complex data from one activity to another. For instance, a user ID retrieved during login can be sent to the HomeActivity to load personalized content.
2.5. Hosting Fragments and Supporting Modularity
Activities can act as containers for fragments, allowing developers to create modular, reusable UI components. This is especially useful for tablet UIs or master-detail layouts.
How Android Activity Works: Internal Architecture

An Activity in Android is not just a UI container. It is deeply integrated into the Android application lifecycle and is governed by the ActivityManagerService (AMS), a system service that manages the lifecycle and stack of all activities in the system.
3.1. Core Components of Activity Lifecycle
- onCreate(): Called when the activity is first created. Use this method to perform initial setup (e.g., UI binding, ViewModel setup).
- onStart(): The activity becomes visible to the user.
- onResume(): The activity is in the foreground and interactive.
- onPause(): The activity is partially obscured by another (e.g., a dialog).
- onStop(): The activity is no longer visible.
- onDestroy(): Cleanup is done here before the activity is destroyed.
- onRestart(): Called after
onStop()
if the activity is restarting.
3.2. Activity Back Stack and Task Management
Activities are arranged in a stack structure. When a new activity is launched, it is pushed onto the stack. Pressing the back button pops the top activity, revealing the previous one. Task affinity, launch modes, and flags help manage complex navigation.
3.3. Context and Resource Access
Each activity has access to a Context object that allows access to:
- Application resources (strings, images)
- Services (Location, Vibration)
- System features (Camera, Sensors)
Basic Workflow of Android Activity
- Initialization: Activity is launched via an intent.
onCreate()
is triggered. Set content view and initialize components. - Interaction:
onStart()
andonResume()
make the UI available for interaction. - User Action: Users interact with buttons, fields, and other UI components. Activity handles events and updates UI or state.
- Inter-Activity Navigation: Intent-based navigation allows switching between activities and passing data.
- Lifecycle Management: System may pause or stop the activity. You must handle state saving and resource management.
- Termination:
onDestroy()
is called before the activity is removed from memory.
Getting Started Guide: Step-by-Step with Kotlin
Step 1: Create a New Project
- Open Android Studio > New Project > Empty Activity
- Name your project and main activity
- Choose Kotlin as the language
Step 2: Define Layout (activity_main.xml)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="@+id/welcomeText"
android:text="Welcome to My App"
android:textSize="24sp"
android:layout_margin="16dp"/>
<Button
android:id="@+id/nextButton"
android:text="Go to Next Screen" />
</LinearLayout>
Step 3: Kotlin Activity Code (MainActivity.kt)
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val button = findViewById<Button>(R.id.nextButton)
button.setOnClickListener {
val intent = Intent(this, SecondActivity::class.java)
startActivity(intent)
}
}
override fun onStart() {
super.onStart()
Log.d("MainActivity", "Started")
}
override fun onPause() {
super.onPause()
Log.d("MainActivity", "Paused")
}
}
Step 4: Create a Second Activity
- Create
SecondActivity.kt
- Define
activity_second.xml
- Register it in
AndroidManifest.xml
class SecondActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second)
}
}
Step 5: Handle Lifecycle Events
Override lifecycle methods to log transitions and manage resources:
override fun onStop() {
super.onStop()
Log.d("MainActivity", "Stopped")
}
override fun onDestroy() {
super.onDestroy()
Log.d("MainActivity", "Destroyed")
}
Advanced Tips and Best Practices
6.1. Use ViewModel for State Preservation
Avoid storing UI-related data in the activity directly. Use ViewModel
for lifecycle-aware state management.
6.2. Avoid Memory Leaks
Always clean up references to large objects (bitmaps, sensors, services) in onDestroy()
.
6.3. Support Configuration Changes
Use onSaveInstanceState()
to persist data during events like device rotation.
6.4. Prefer Navigation Component
Instead of manually managing activities and fragments, use Jetpack Navigation to handle transitions, back stack, and argument passing declaratively.