Devesh Rx Logo
Devesh Rx Blog

Android Activity & Lifecycle Explained (Part 3)

March 18, 2026

Android Activity & Lifecycle Explained (Part 3)

This video dives into the core concept of Android Activities, explaining what they are, how to create them, and how they function within an application. Think of activities as the individual screens users interact with in your app – each one is an activity! Let’s explore this fundamental component of Android development.

What Exactly is an Android Activity?

When you launch an Android application, the screen you see is an Android Activity. Every screen within your app, including login screens, checking screens, and more, is considered a separate activity. Essentially, an Android Activity is like a foreground process – it’s the part of the app currently visible to the user.

The contact and phone applications are great examples. The first screen you see when opening either is an activity. Each screen in your application can be thought of as its own distinct activity. It’s important to note that activities are not background processes; they’re always on the front screen, actively engaging with the user.

The Main Activity: Your App’s Entry Point

When you create an Android project with Android Studio, a basic activity file is automatically generated – this is your main activity. This file serves as the entry point for your application. When you hit the debug button in Android Studio, the main activity launches with the “Hello World” screen.

Understanding the Activity Lifecycle

An Android Activity goes through a series of states throughout its existence, which is known as the activity lifecycle. These states are managed by different functions:

  • onCreate(): This function is called when the activity is first created.
  • onStart(): Called when the activity is about to become visible to the user.
  • onResume(): Called when the activity is running and interactive.
  • onPause(): Called when the activity is no longer in focus, but still visible.
  • onStop(): Called when the activity is no longer visible to the user.
  • onDestroy(): Called when the activity is being destroyed.

These functions allow you to perform specific actions at different points in an activity’s lifecycle.

Creating a New Activity: A Step-by-Step Guide

Let’s create a brand new activity within our project. There are two ways to do this: using Android Studio templates or manually creating a class file.

Method 1: Using Templates (Easier)

  1. Right-click on your package name in the Project window.
  2. Select “New” -> “Activity”.
  3. Choose an activity template (e.g., Empty Activity).
  4. Enter a name for your activity and click “Finish.”

Method 2: Manual Creation (More Understanding)

  1. Right-click on your package name in the Project window.
  2. Select “New” -> “Kotlin Class/File”.
  3. Name the class, for example, “MyActivity2.”
  4. Make sure to extend the androidx.appcompat.app.AppCompatActivity class (or another appropriate activity class).
  5. Create an onCreate() function.

Important: Enabling Edge-to-Edge

Within your onCreate() function, add this line:

enableEdgeToEdge()

This is crucial for modern Android devices to prevent the app’s UI from being overlapped by the system UI (like the status bar and navigation bar).

Building a Simple UI with Jetpack Compose

You can create your user interface using either XML layouts or Jetpack Compose, Android’s modern UI toolkit. Here’s how you might build a simple UI in Kotlin using Jetpack Compose:

setContent {
    Column() {
            Text(
                text = "My Activity 2 Screen",
                modifier = Modifier.padding(20.dp)
            )
        }
}

The Column layout arranges UI elements vertically. The Text widget displays “My Activity 2 Screen” on the screen.

Declaring the New Activity in the Manifest File

To make your new activity visible to the system, you need to declare it in the AndroidManifest.xml file.

  1. Open AndroidManifest.xml.
  2. Inside the <application> tag, add an <activity> tag for your new activity:
<activity android:name=".MyActivity2"
            android:label="My Activity 2"
            android:exported="false">
</activity>

The android:name attribute specifies the class of the activity. android:label sets the name that appears in the device settings, and android:exported="false" prevents other applications from launching this activity (set to “true” if you want to allow external apps to use it).

Launching a New Activity Programmatically

To open another activity from within an existing one, you’ll need to use an Intent. Here’s how:

  1. In your main activity, add a button with the text “Open My Activity”.
  2. Set an onClick event for the button.
  3. Inside the onClick event, create an Intent:
val intent = Intent(this, MyActivity2::class.java)
startActivity(intent)

This creates an intent to start your new activity and then uses startActivity() to launch it.

Recap & Further Learning

In this video, you learned how to:

  • Create a new Android Activity.
  • Declare the activity in the Android Manifest.xml file.
  • Launch a new activity using Intents.
  • Understand the different states of an Android Activity (activity lifecycle).

For a deeper understanding of Android app development fundamentals, check out the complete playlist linked given below!

Remember, mastering activities is essential for building any Android application. Keep practicing, and you’ll be creating complex and engaging apps in no time!

📌 Full Course Playlist https://www.youtube.com/playlist?list=PLO1OrQEU0vHNmD9Xqzs-qXwzzwrDvdhVu

#Tutorial
0 Introduction
1 Setting up Android Studio IDE
2 Mastering Android Studio: Navigating the IDE & Project Structure
3 Android Activity & Lifecycle Explained
4 Android Services: Background, Foreground, and Bound Services Explained
5 Android Broadcast Receivers: The Complete Guide to Listening and Sending Events

~ ~ THANK YOU FOR READING ~ ~

Share: