What Is an Activity in Android? Complete Beginner-Friendly Explanation

What Is an Activity in Android? Lifecycle, Examples, and Usage

An Activity is one of the most fundamental building blocks of any Android app. Every time you open an app and see a screen with which you can interact, you are usually looking at an Activity. Understanding what an Activity is, how its lifecycle works, and how it fits with other components like Fragments is essential for building stable, user-friendly Android applications.

What Is an Activity in Android?

An Activity is an application component that provides a single, focused screen where the user can perform a specific task. It manages a window on the device and hosts the user interface, such as buttons, text fields, images, and fragments.

In simple terms, you can think of an Activity as one page of your app. Examples include a login screen, a home screen, a settings screen, or a detail screen. An app can have one Activity or multiple Activities, and users move between them as they navigate through the app.


Role of Activities in an Android App

Activities act as the main entry point for interacting with your app. When the user taps your app icon in the launcher, the system starts an Activity (often called the “main” or “launcher” Activity). From there, that Activity can start other Activities using Intents.

Each Activity is responsible for:

  • Displaying UI elements (or hosting Fragments that display the UI).
  • Responding to user input such as taps, text input, and gestures.
  • Coordinating navigation to other Activities or Fragments.
  • Managing its lifecycle so resources are allocated and released at the right time.

Activity and Intent: How Activities Start

Android uses Intents to start Activities and pass data between them. An Intent is a messaging object that describes what you want to do, such as “open this screen” or “share this text”.

For example, from one Activity you might open another Activity like this:

val intent = Intent(this, DetailActivity::class.java)
intent.putExtra("item_id", 42)
startActivity(intent)

Here, the current Activity starts DetailActivity and passes an item_id extra. The new Activity can read that extra and show the correct content.

Activity Lifecycle: The Big Picture

Activities do not just appear and disappear. They go through a series of well-defined lifecycle states as the user opens the app, rotates the device, presses Home, or navigates back. Android provides callback methods so you can respond to these state changes.

The core Activity lifecycle methods you will use most often are:

  • onCreate() – Called when the Activity is first created.
  • onStart() – Called when the Activity becomes visible to the user.
  • onResume() – Called when the Activity moves to the foreground and the user can interact with it.
  • onPause() – Called when the Activity is partially obscured (for example, another Activity appears on top).
  • onStop() – Called when the Activity is no longer visible.
  • onDestroy() – Called before the Activity is destroyed and removed from memory.

By overriding these methods, you can perform setup, save state, release resources, or stop background work at the appropriate times.


Detailed Look at Key Lifecycle Methods

onCreate()

onCreate() is the first callback that runs when an Activity instance is created. It is where you set up your initial state: inflate the layout, initialize variables, set up ViewBinding or DataBinding, attach listeners, and optionally restore previously saved state.

A common pattern in Kotlin looks like this:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    // Initialize views, set up click listeners, attach ViewModel, etc.
}
  

onStart() and onResume()

onStart() is called when the Activity becomes visible but the user might not yet be able to interact. Shortly after, onResume() is called when the Activity is in the foreground and has user focus.

This is a good place to start animations, resume cameras, re-register listeners, or restart anything that should run while the Activity is visible and interactive.

onPause() and onStop()

onPause() is invoked when the system is about to put your Activity in the background (for example, another Activity is opening on top). You should use this to pause ongoing actions that should not continue while the user is not actively interacting, such as pausing a video or stopping location updates.

onStop() is called when the Activity is no longer visible at all. This is the right place to release heavier resources like large bitmaps, database connections, or sensors that you do not need when the Activity is off-screen.

onDestroy()

onDestroy() is called when the Activity is about to be destroyed. This could be because the user finishes the Activity (for example, by pressing Back) or because the system destroys it to free memory.

In onDestroy(), you should perform any final cleanup that has not already been handled in onStop() or onPause(), such as unregistering any remaining listeners or releasing last resources.

Activity States and the Back Stack

Android manages Activities using a back stack. When you start a new Activity, the previous one is pushed to the back stack. When the user presses the system Back button, the current Activity is popped from the stack and destroyed, and the previous Activity is brought to the foreground.

From the user’s perspective, this feels like moving forward and backward through screens. As a developer, you can rely on this default behavior or customize it (for example, finishing an Activity programmatically using finish() when a flow is complete).

Example: Simple Activity Implementation

Let’s look at a simple example of an Activity that displays a message and reacts to a button click. First, define the layout in 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:padding="16dp">

    <TextView
        android:id="@+id/tvGreeting"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello, Activity!"
        android:textSize="20sp" />

    <Button
        android:id="@+id/btnChangeText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Change Text" />

</LinearLayout>
  


Next, implement the MainActivity.kt class:

import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val tvGreeting: TextView = findViewById(R.id.tvGreeting)
        val btnChangeText: Button = findViewById(R.id.btnChangeText)

        btnChangeText.setOnClickListener {
            tvGreeting.text = "Button clicked in Activity!"
        }
    }
}
  

Here, MainActivity is responsible for displaying the layout, finding views, and handling user interaction. Android creates this Activity when the app starts, calls onCreate(), and shows the UI to the user.

Activity vs Fragment: How They Work Together

Activities and Fragments often work hand in hand. In many modern apps, the Activity acts as a container or “host”, while most UI logic lives inside Fragments. The Activity manages high-level tasks like navigation, toolbars, and handling system actions, while Fragments handle the details of each screen’s content.

For example, a single MainActivity might host a HomeFragment, DetailsFragment, and SettingsFragment at different times. This “single-Activity, multi-Fragment” approach keeps navigation and lifecycle management centralized while making the UI more modular and reusable.

To learn more about Activity vs Fragment, we’ve already published a detailed article on this topic. Please refer to that article for an in-depth explanation.

Best Practices for Working with Activities

To write clean, maintainable Android apps, follow these best practices when working with Activities:

  • Keep Activities as slim as possible; delegate complex UI logic to Fragments and business logic to ViewModels or other classes.
  • Use lifecycle-aware components (such as ViewModel and LiveData/Flow) to avoid manual lifecycle management where possible.
  • Avoid doing heavy work directly in lifecycle callbacks; offload long operations to background threads or coroutines.
  • Always consider configuration changes (like rotation) and use onSaveInstanceState() or ViewModel to preserve important UI state.
  • Do not leak Activities; avoid holding strong references to an Activity in long-lived objects like singletons or static fields.

Conclusion

An Activity in Android is much more than just a screen—it is the core component that manages the user’s interaction with your app, orchestrates navigation, and coordinates UI elements and Fragments. By understanding what an Activity is, how the lifecycle works, and how to combine Activities with other components, you can build apps that are robust, responsive, and easy to maintain.

With the concepts and examples in this guide, you are ready to design and implement Activities that form the backbone of your Android applications, from simple single-screen apps to complex, multi-screen experiences.

Post a Comment

0 Comments