Skip to content
Tutorials Center
My learning

Topics

Lesson 13 of 104 ยท Activities and the Lifecycle

Activities and setContent

Learn what an Android Activity is, how ComponentActivity and setContent put Compose on screen, what edge-to-edge means and why modern apps use one activity.

Every Android app is a collection of components that the operating system starts and stops for you. Your code never has a main function that the system calls once; instead, the system creates an object of one of your classes when it needs it. The component that puts a screen in front of the user is the Activity. When you tap an app icon, Android creates an activity object and asks it to show its UI.

This lesson explains what an activity is, how setContent connects it to Jetpack Compose, what edge-to-edge means, and why most modern apps have only one activity. The real code needs the Android SDK and an emulator or a device, so the Android and Compose snippets here are read-only. The runnable TryIt blocks are plain Kotlin stand-ins: they model a task stack and an activity that owns its content, so you can see the behavior without a phone. We say clearly which is which. If you have not seen Compose yet, Jetpack Compose basics is a short introduction, and Thinking in Compose comes later in this track.

What an Activity is

An Activity is a class that represents one window of your app that the user can see and interact with. You do not create it with MainActivity() yourself. You declare it in the manifest, and Android instantiates it, calls its lifecycle methods and destroys it. (The manifest is covered in The Manifest and Resources.)

A minimal activity looks like this:

Kotlin
// read-only example: needs the Android SDK
class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            Text("Hello, Android!")
        }
    }
}

and it is registered like this:

XML
<!-- read-only example: AndroidManifest.xml -->
<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>

The intent-filter with MAIN and LAUNCHER is what makes this activity appear as the app icon on the home screen. android:exported="true" is required on Android 12 and later for any activity that has an intent filter, because other apps (here, the launcher) may start it.

Three things are worth noticing:

  • The class extends ComponentActivity, not the bare Activity. ComponentActivity is the base class from AndroidX that Compose, the ViewModel library, the Activity Result API and the back-press dispatcher all build on. AppCompatActivity and FragmentActivity extend it, so they have everything it has.
  • onCreate is a lifecycle callback. The system calls it once when the activity is created. The next lesson, The Activity Lifecycle, covers all of them.
  • savedInstanceState is a Bundle that is null the first time and carries saved data when the system recreates the activity. Configuration Changes and Saved State explains it.

setContent: where Compose starts

setContent { ... } is an extension function from androidx.activity.compose. It creates a ComposeView, installs it as the content of the activity's window and runs your composable lambda inside it. Everything inside the lambda is Compose: from that point on, you describe the UI with composable functions.

Kotlin
// read-only example: needs the Android SDK and Compose
class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            MyAppTheme {
                Surface(modifier = Modifier.fillMaxSize()) {
                    HomeScreen()
                }
            }
        }
    }
}

Keep the activity thin. Its job is to be the entry point: set the theme, call setContent, and hand over to composable functions. Business logic, data loading and screen state belong in other classes (you will build them in the App Architecture chapter), because an activity can be destroyed and recreated at any time, for example when the user rotates the phone.

The next TryIt is a stand-in. FakeActivity plays the role of ComponentActivity: the "system" calls onCreate, the subclass calls setContent with a lambda, and the activity remembers the lambda so it can run it when the window is shown. This is much simpler than the real implementation, but the shape is the same: the activity owns the content, and the content is a function.

Kotlin An activity that owns its content (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with readln()), it gets the next line from this box.

The second activity shows the classic beginner mistake: if you never call setContent, the window is simply blank.

Edge-to-edge

On Android 15 (API 35) and later, apps targeting that version are drawn edge-to-edge by default: the window extends behind the status bar at the top and the navigation bar (or gesture area) at the bottom. On older versions you opt in by calling enableEdgeToEdge() before setContent, and the same function works on every version.

Drawing behind the system bars looks modern, but it means your content can end up under them. Compose gives you window insets to handle it. The Material 3 Scaffold applies the right padding for you through the innerPadding it passes to your content, and you can also use modifiers such as Modifier.safeDrawingPadding() or Modifier.windowInsetsPadding(...). The rule of thumb: backgrounds may flow under the bars, but text and buttons must not. Adaptive Layouts and Window Size Classes goes deeper into insets.

Many activities or one?

In the View era, apps often had one activity per screen: LoginActivity, ListActivity, DetailActivity. Each switch started a new activity through an Intent and pushed it on the task back stack, the stack of activities the user can go back through with the back gesture.

Today the recommendation is a single-activity architecture: one MainActivity hosts all screens, and Compose navigation swaps the content (Navigation Compose Basics). The reasons:

  • Sharing state and animating between screens is far easier inside one composition than across windows.
  • Starting an activity is expensive; switching composables is cheap.
  • Fewer manifest entries and fewer places where lifecycle bugs can hide.

Extra activities still make sense for real entry points that other apps or the system start, such as a share target, a payment screen or a picture-in-picture video activity.

flowchart LR
    A["Launcher icon"] --> B["MainActivity<br/>(setContent)"]
    B --> C["NavHost"]
    C --> D["HomeScreen"]
    C --> E["DetailScreen"]
    C --> F["SettingsScreen"]

One activity hosts the whole app; screens are composables swapped by navigation.

The task back stack

The system groups activities into tasks. Opening an app starts a task whose bottom entry is the launcher activity; each new activity started within it is pushed on top; the back gesture pops the top one and destroys it. By default the same activity class can be pushed several times (the standard launch mode). Setting launchMode="singleTop" in the manifest (or the intent flag FLAG_ACTIVITY_SINGLE_TOP) tells the system: if this activity is already on top, deliver the new intent to it instead of creating another copy.

The stand-in below is a task stack with those two modes. It is a model of the rule, not the Android implementation.

Kotlin Launching activities on a task stack (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with readln()), it gets the next line from this box.

Notice that two Detail entries stay after the second start. That duplicate is exactly why the single-activity design and launchSingleTop navigation options exist: they stop the stack from filling up with copies of the same screen.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Who creates the instance of your MainActivity class?

    You declare the activity in the manifest and Android instantiates it, calls its lifecycle callbacks and destroys it. You never construct it yourself.
  2. 2. What does setContent do in a ComponentActivity?

    setContent creates a ComposeView, makes it the window content and runs your composable lambda in it. Without the call the window stays blank.
  3. 3. Why is a single-activity architecture recommended with Compose?

    One activity hosting a navigation graph avoids the cost of starting windows and keeps shared state and animations in one place. Multiple activities are still allowed for real external entry points.
  4. 4. What does edge-to-edge mean?

    Edge-to-edge extends the window behind the status and navigation bars. Backgrounds can flow under them, but you apply insets so text and buttons are not covered.

Summary

  • An Activity is a window the system creates for your app from a manifest declaration; you never construct it yourself.
  • Extend ComponentActivity; it is the base for Compose, ViewModel, Activity Result and back handling.
  • setContent { ... } is where Compose starts. Keep the activity thin: theme, setContent, then composables.
  • Call enableEdgeToEdge() and respect window insets so content is not hidden under system bars.
  • Prefer one activity with Compose navigation; add more only for real external entry points.
  • Activities live on a task back stack; singleTop avoids stacking duplicates.
  • The Android code in this lesson is read-only; the TryIt blocks are stand-ins that model the ideas.

Next: The Activity Lifecycle