Table of Contents
The Role of `MainActivity` in Your First App
When you create a new Android project in Android Studio, the template gives you a starting point. At the center of that starting point is a Kotlin file called MainActivity.kt. This file defines a class named MainActivity, and it is usually the first screen your user will see when they open your app for the very first time.
MainActivity represents a single screen of your app. Android calls such a screen an Activity. For now, you can think of MainActivity as the entry door to your app, which Android uses when the user taps your app icon.
In a brand new project, Android Studio generates this class automatically, using a template you selected when creating the project. You do not need to write it from scratch, but you do need to understand what is inside it and why it is there.
MainActivity is the default launcher activity. Android starts it automatically when the user taps the app icon, because it is marked as the main entry point.
Later in the course, other screens will use the same Activity concept. For now, you only work with MainActivity and learn how it connects code and layout.
Typical Structure of `MainActivity.kt`
Open app > java > your.package.name > MainActivity.kt in Android Studio. In a simple template like Empty Activity, you will see something similar to this:
package com.example.myfirstapp
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}Each part has a specific purpose. You do not need to master every keyword yet, but you should recognize the overall pattern, since almost every Activity will look similar.
The file starts with the package line, which tells Kotlin where this class lives in your project structure. The import lines tell the compiler which Android classes MainActivity uses. The main part is the MainActivity class itself and its onCreate function, which is where your Activity is first set up.
`MainActivity` as a Kotlin Class
MainActivity is a normal Kotlin class that extends another class named AppCompatActivity. The line
class MainActivity : AppCompatActivity() {
// ...
}
declares this relationship. By extending AppCompatActivity, MainActivity gains behavior that Android expects from a screen, such as being able to show a user interface, handle rotation, and react when the user leaves or returns to the app.
You will learn more about classes and inheritance in dedicated chapters. For this chapter, the important point is that your Activity must inherit from an appropriate Activity base class so that Android can manage it correctly.
The `onCreate` Function: Where Setup Begins
Inside MainActivity, one function appears in every template:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
This function is called by Android when the Activity is first created. Think of onCreate as your starting hook. It is where you usually set up the user interface, connect code to views, and initialize basic data.
The override keyword means that MainActivity is providing its own specific version of a function that already exists in AppCompatActivity. That original version defines default behavior. By overriding it, you can add or change what happens at creation time.
The savedInstanceState parameter can hold data from a previous instance of this Activity, for example after a configuration change. You will revisit this in later chapters about Activity lifecycle and configuration changes. In your very first apps, you will often ignore it.
Always call super.onCreate(savedInstanceState) as the first line inside onCreate. This ensures the base AppCompatActivity can perform its essential setup before you customize anything.
If you forget to call super.onCreate, your Activity might crash or behave in unexpected ways.
Connecting Code to Layout with `setContentView`
The key line that makes your Activity show something on screen is:
setContentView(R.layout.activity_main)
This tells Android which layout XML file should be used for the user interface of this Activity. When you created the project, Android Studio generated a layout file in the res/layout directory, usually named activity_main.xml.
R.layout.activity_main refers to that XML file. The R class is generated automatically and contains references to all your app resources such as layouts, strings, and images. The layout name activity_main comes from the file name activity_main.xml.
After you call setContentView, the views defined in activity_main.xml are loaded into memory and become visible on the screen when the Activity is displayed. Once this is done, you can start finding individual views from that layout and working with them in Kotlin code, for example to read text, handle button clicks, or update what the user sees.
Without setContentView, your Activity would have no user interface. It would exist in the system but show only a blank window.
`MainActivity` and `AndroidManifest.xml`
Although this chapter focuses on the Kotlin file, MainActivity is also mentioned in AndroidManifest.xml. The manifest is where you declare your Activities so that Android knows they exist. For the first Activity, the manifest also marks it as the launcher Activity.
A typical manifest entry for MainActivity looks like this:
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
This entry tells Android that MainActivity is the main entry point of your app and should appear as an icon in the launcher. You will study the manifest and intent filters later. For now, it is enough to know that this connection is what causes MainActivity to start when the app icon is tapped.
Adding Your Own Logic Inside `MainActivity`
Once you understand the basic structure, MainActivity becomes the place where you add initial app behavior. After setContentView, you can obtain references to views from your layout and react to user actions.
For example, in a simple app, you might find a button and change what happens when the button is pressed:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val myButton = findViewById<Button>(R.id.my_button)
myButton.setOnClickListener {
// Your code that runs when the button is tapped
}
}
Here, findViewById looks for a view with the ID my_button in the layout you set earlier, and setOnClickListener attaches behavior to it. You will learn more about buttons, click listeners, and user interaction in later chapters, but this example shows how MainActivity usually hosts the first pieces of your app logic.
Over time, as your app grows, MainActivity often becomes a coordinator that talks to other components such as Fragments, ViewModels, or navigation controllers. At the very beginning, it is common for much of your logic to live directly inside this Activity.
Understanding Template Variations
Different project templates in Android Studio may generate slightly different versions of MainActivity. For example, a Basic Activity template might include a toolbar setup, a floating action button, or navigation components. A Compose template creates a MainActivity that sets up a Compose UI instead of a traditional XML layout.
Despite these differences, the core idea remains the same. MainActivity is still a class that extends ComponentActivity or AppCompatActivity, still has an onCreate function, and still sets up the initial user interface for your app.
When you explore a new template, look for the overridden onCreate method and identify where the UI is being created or set. In XML based templates, it is usually setContentView. In Compose templates, it might be inside a setContent block.
Summary of `MainActivity` Responsibilities
By now you should see MainActivity as the starting screen controller of your app. It is created by the template so your app can run immediately, but understanding its structure is essential before you modify it.
MainActivity extends an Activity base class, overrides onCreate to perform setup, calls super.onCreate to keep the system behavior intact, and uses setContentView to connect XML layout to the screen. It is registered in the manifest as the launcher Activity so that Android knows to open it when the user starts your app.
As you build more features, you will keep returning to MainActivity to attach logic to UI elements, navigate to other screens, and integrate other components. Learning to read and reason about this file is a key step in becoming comfortable with Android development.