Android Fundamentals: Activities & Lifecycle
Behind every Compose screen sits an Activity with a well-defined lifecycle . Understanding when the system creates, pauses, and destroys your screen is essential to avoid bugs and leaks.
Learn Android Fundamentals: Activities & Lifecycle in our free Kotlin course — a beginner-friendly interactive lesson with worked examples, a practice…
Part of the free Kotlin course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
By the end you'll trace the lifecycle callbacks, see why a ViewModel survives rotation, and launch coroutines safely with viewModelScope .
What You'll Learn in This Lesson
1️⃣ The Activity Lifecycle
An Activity is a single screen. The OS calls lifecycle methods in a predictable order: onCreate then onStart then onResume on the way up, and onPause then onStop then onDestroy on the way down.
Use each callback for the right job: one-time setup in onCreate , acquiring resources in onStart / onResume , releasing them in onPause / onStop .
2️⃣ ViewModel and viewModelScope
A ViewModel holds UI state that survives configuration changes like rotation. Launch asynchronous work in viewModelScope so it's cancelled automatically when the ViewModel is cleared.
Your turn. Replace the TODO , then run and read Logcat.
Build a ViewModel that holds a name in state and explain why it survives rotation.
📋 Quick Reference — Lifecycle
Practice quiz
What is an Activity in Android?
- A background service
- A single, focused screen the user interacts with
- A database table
- A build configuration
Answer: A single, focused screen the user interacts with. An Activity represents one screen and is an entry point for user interaction.
Which lifecycle callback runs first when an Activity is created?
- onResume()
- onStart()
- onCreate()
- onPause()
Answer: onCreate(). onCreate() is the first callback, where you do one-time setup like setContent.
Which callback runs when the Activity is about to become visible but not yet interactive?
- onStart()
- onDestroy()
- onPause()
- onStop()
Answer: onStart(). onStart() runs as the Activity becomes visible, before onResume() makes it interactive.
Which callback signals the Activity is in the foreground and interactive?
- onCreate()
- onResume()
- onStop()
- onRestart()
Answer: onResume(). onResume() is called when the Activity reaches the foreground and can take input.
When another activity comes to the foreground, which is called first on the current one?
- onDestroy()
- onCreate()
- onPause()
- onStart()
Answer: onPause(). onPause() runs first as the Activity loses focus, before onStop().
Which callback is the last one before an Activity is destroyed?
- onPause()
- onStop()
- onFinish()
- onDestroy()
Answer: onDestroy(). onDestroy() is the final callback before the Activity is torn down.
What is the main purpose of a ViewModel?
- To draw UI directly
- To hold and manage UI-related state across configuration changes
- To replace the manifest
- To store passwords
Answer: To hold and manage UI-related state across configuration changes. A ViewModel holds UI state and survives configuration changes like rotation.
Why does a ViewModel survive screen rotation when an Activity does not?
- It is stored on disk
- It is a singleton
- It is scoped to the lifecycle owner and retained across configuration changes
- Rotation never destroys anything
Answer: It is scoped to the lifecycle owner and retained across configuration changes. The ViewModel is retained by the framework across configuration changes, unlike the Activity instance.
What is viewModelScope used for?
- Defining XML layouts
- A CoroutineScope tied to the ViewModel's lifecycle for launching coroutines
- Storing the back stack
- Inflating menus
Answer: A CoroutineScope tied to the ViewModel's lifecycle for launching coroutines. viewModelScope launches coroutines that are automatically cancelled when the ViewModel is cleared.
What happens to coroutines in viewModelScope when the ViewModel is cleared?
- They are paused forever
- They keep running in the background
- They are automatically cancelled
- They restart
Answer: They are automatically cancelled. When the ViewModel is cleared (in onCleared), viewModelScope cancels its coroutines to avoid leaks.