Android Beginners

Lesson 7: Understanding Activities and the App Lifecycle

Ahmad Najar Ahmad Najar 1 min read

Understanding Activities and the App Lifecycle

MainActivity extends ComponentActivity — but what actually is an Activity, and why does onCreate exist?

What an Activity is

An Activity represents one "screen" the operating system knows about and can manage — it's the unit Android uses to track what's currently visible, what's in the background, and what can be safely shut down to free memory. Most simple apps (including ours) only ever need one Activity, with Compose handling navigation between different UI screens inside it — we'll cover that properly in lesson 15.

The lifecycle: what happens and when

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // Called once when the Activity is first created
    }

    override fun onStart() {
        super.onStart()
        // Called when the Activity becomes visible
    }

    override fun onResume() {
        super.onResume()
        // Called when the user can interact with it
    }

    override fun onPause() {
        super.onPause()
        // Called when partially obscured (e.g. a dialog appears)
    }

    override fun onStop() {
        super.onStop()
        // Called when no longer visible
    }
}

Why this matters for a beginner

You won't override most of these methods directly in a Compose app — Compose and its associated lifecycle-aware components handle most of this automatically. But understanding that Android can pause, stop, and even fully destroy your Activity (say, when the user rotates the screen, or the OS needs memory) explains a specific, common beginner confusion: why data can seem to "disappear" on rotation if you're not storing it correctly. We'll solve that properly with ViewModel in lesson 17.

The key takeaway

Your Activity is not guaranteed to live forever while your app is open — treat anything you don't want to lose as something that needs to be deliberately preserved, not something that just persists by default.

Next: Lesson 8 — arranging UI elements with Column, Row, and Box.

Android Beginners