If you have already installed Android Studio and explored the basic project structure, you are ready for the next crucial step in Android app development. In this guide, we will explore the fundamental concept of the Android Activity. We will break down what an Activity is, how its lifecycle works, and how you can manually create and navigate between multiple activities in your app.
What is an Android Activity?
When you launch any application, the screen you see on your device is an Android Activity. Whether it is a login screen, a checkout page, or your phone’s contact list, each of these individual screens is considered an Activity.
In simple terms, an Activity is a foreground process that you interact with on your screen. Note the key difference here: unlike a background process, an Activity is always front and center for the user. In our default “Hello World” project, Android Studio automatically generates a MainActivity file, which serves as the visual entry point for the application.
Under the hood, an Activity is simply a Kotlin class. In modern development, this class generally inherits from ComponentActivity (though alternatives like AppCompatActivity exist).
Understanding the Android Activity Lifecycle
Every Activity goes through a specific set of events from the moment it is launched until it is closed. This is known as the Activity Lifecycle.
Inside your Activity class, you will find multiple functions tied to these lifecycle events:
- onCreate: Called whenever an Android application activity is first created.
- onStart: Called when the Activity becomes visible to the user.
- onResume: Called when the user begins interacting with the application.
- onPause: Called when the Activity is paused (e.g., partially obscured or moving to the background).
- onStop: Called when the Activity is no longer visible.
- onDestroy: Called when the user completely kills or destroys the Android Activity.
Designing UI with Jetpack Compose
Every Activity requires its own User Interface (UI). While you can use the good old XML layouts with drag-and-drop elements, modern Android app development relies heavily on Jetpack Compose.
Instead of dragging and dropping UI elements, Jetpack Compose allows you to build your UI using code. For example, using the setContent function, you can write a MainScreen composable function. Inside it, you can use layouts like a vertical Column and widgets like a Text function to display “Hello World” on the screen.
How to Create a New Android Activity
Most Android applications require multiple activities (e.g., a login screen, a sign-up screen, a home screen, and an about screen). Let’s look at how to create a brand new Activity, which we will call MyActivity2.
The Template Method
Android Studio provides built-in templates for quick creation:
- Right-click on your package name.
- Go to New > Activity.
- Select a template (such as Empty Activity), click Next, name it, and click Finish.
The Manual Method
To better understand how Android works under the hood, it is highly recommended to build an Activity manually.
- Step 1: Create a Kotlin Class
Right-click your package name, go to New > Kotlin Class/File, and name it
MyActivity2. - Step 2: Extend ComponentActivity Make this simple Kotlin class an Activity by extending it with ComponentActivity.
- Step 3: Add the onCreate Function
Override the
onCreatefunction. - Step 4: Enable Edge-to-Edge
Inside
onCreate, add the enableEdgeToEdge() line. This step is incredibly important for modern Android devices, as it prevents your application’s UI and the device’s system UI from overlapping. - Step 5: Set the Content
Finally, use
setContentto add your Jetpack Compose UI (like a Text widget reading “My Activity 2 Screen”).
Declaring the Activity in the Android Manifest
Simply creating an Activity class isn’t enough; you must register it so the Android Operating System knows it exists. You do this in the AndroidManifest.xml file.
- Open AndroidManifest.xml.
- Locate the
<application>tag. - Inside the application tag, create an
<activity>tag. - Add the property
android:nameand point it to your new class (e.g.,MyActivity2). - Add a
labelproperty to give the Activity a name. - Set the
exportedproperty to eithertrueorfalse.- Setting it to true tells the Android OS that third-party applications are allowed to trigger and open this Activity.
- Setting it to false restricts access so that only your own app can open it.
Note: If you look at your default MainActivity inside the manifest, you will notice an intent-filter with the category launcher and action main. These two lines tell the OS that this specific Activity is the main entry point when a user taps your app icon.
How to Open a New Activity Programmatically
Now that our new Activity is created and registered, how do we open it? We can do this programmatically using an Intent.
- Go back to your
MainActivity. - Add a UI Button with the text “Open My Activity”.
- Add an onClick event to the button.
- Inside the onClick event, create an Intent pointing to the
MyActivity2class. - Grab the current screen context using
LocalContext.current. - Finally, pass the Intent into the
startActivity()function using that context.
Once you click the debug button and open the Android Virtual Device, your button will appear. Clicking it will seamlessly transition you from MainActivity to MyActivity2. To go back to your “Hello World” home screen, simply press the back button!
Summary
Understanding Activities is essential for building robust Android applications. Let’s recap the core fundamentals covered in this guide:
- Creating a New Activity: We learned how to manually build an Activity (
MyActivity2) using Kotlin and Jetpack Compose. - The Manifest File: We discovered the importance of declaring every new Activity inside the AndroidManifest.xml file.
- Intents: We successfully navigated between two Activities using an Intent triggered by a button press.
- Activity Lifecycle: We explored the various events of an Activity—from onCreate all the way to onDestroy.
Mastering these concepts will serve as a strong foundation as you continue your journey into Android app development!
📌 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 |
~ ~ THANK YOU FOR READING ~ ~