🟢 Getting Started · Lesson 07
Activity & Lifecycle
What is an Activity?
An Activity is one screen of your app — a class that extends AppCompatActivity and shows an XML layout. Android, not you, decides when it is created, shown, hidden and destroyed. It tells you through lifecycle callbacks.
The lifecycle diagram
Log every callback
Kotlin – MainActivity.kt
class MainActivity : AppCompatActivity() {
private val TAG = "Lifecycle"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Log.d(TAG, "onCreate – build the UI once")
}
override fun onStart() { super.onStart(); Log.d(TAG, "onStart – visible") }
override fun onResume() { super.onResume(); Log.d(TAG, "onResume – user can interact") }
override fun onPause() { super.onPause(); Log.d(TAG, "onPause – losing focus") }
override fun onStop() { super.onStop(); Log.d(TAG, "onStop – not visible") }
override fun onRestart() { super.onRestart(); Log.d(TAG, "onRestart – coming back") }
override fun onDestroy() { super.onDestroy(); Log.d(TAG, "onDestroy – gone") }
}Run the app and open Logcat (bottom bar), filter by tag:Lifecycle:
Logcat – app opened
D/Lifecycle: onCreate – build the UI once
D/Lifecycle: onStart – visible
D/Lifecycle: onResume – user can interact
What happens when…
Logcat – Home, then back
--- press Home ---
D/Lifecycle: onPause – losing focus
D/Lifecycle: onStop – not visible
--- open the app again ---
D/Lifecycle: onRestart – coming back
D/Lifecycle: onStart – visible
D/Lifecycle: onResume – user can interact
Logcat – screen rotation
--- rotate the phone ---
D/Lifecycle: onPause – losing focus
D/Lifecycle: onStop – not visible
D/Lifecycle: onDestroy – gone
D/Lifecycle: onCreate – build the UI once
D/Lifecycle: onStart – visible
D/Lifecycle: onResume – user can interact
| User action | Callbacks |
|---|---|
| Opens app | onCreate → onStart → onResume |
| Presses Home / switches app | onPause → onStop |
| Returns | onRestart → onStart → onResume |
| Presses Back (leaves screen) | onPause → onStop → onDestroy |
| Rotates phone / changes dark mode | full destroy + create |
| Dialog / permission popup appears | onPause only |
Screen rotation and saving state
Kotlin – CounterActivity.kt
class CounterActivity : AppCompatActivity() {
private var count = 0
private lateinit var tvCount: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_counter)
tvCount = findViewById(R.id.tvCount)
// restore after rotation (savedInstanceState is null on the first launch)
count = savedInstanceState?.getInt(KEY_COUNT) ?: 0
tvCount.text = count.toString()
findViewById<Button>(R.id.btnPlus).setOnClickListener {
count++
tvCount.text = count.toString()
}
}
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putInt(KEY_COUNT, count) // save before the activity is destroyed
}
companion object { private const val KEY_COUNT = "count" }
}For bigger data (lists from a database) use a ViewModel — you will learn it in the MVVM lesson.
What to do in which callback
| Callback | Do this |
|---|---|
| onCreate | setContentView, binding, adapters, click listeners, observe ViewModel |
| onStart / onStop | Start/stop things that matter while visible (location updates, animations) |
| onResume / onPause | Camera preview, sensors, video playback |
| onDestroy | Release resources not tied to lifecycle-aware components |
Common mistakes
- Forgetting
super.onXxx()→SuperNotCalledExceptioncrash. - Heavy work in onCreate on the main thread → black screen / ANR. Use coroutines.
- Assuming onDestroy always runs → save important data earlier.
- Storing a big list in the Bundle →
TransactionTooLargeException; use a ViewModel or database.
Summary
- Six main callbacks: onCreate, onStart, onResume, onPause, onStop, onDestroy (+ onRestart).
- Rotation recreates the Activity; save small state in
onSaveInstanceState. - Set up UI in onCreate; start/stop visible-only work in onStart/onStop.
✅ Practice
Add the logging code to your HelloApp, then press Home, come back, rotate and press Back. Write down the order of callbacks for each case and compare with the table.
Frequently Asked Questions
Why does my data disappear when I rotate the phone?
Rotation destroys and recreates the Activity. Save small values in onSaveInstanceState, or keep data in a ViewModel which survives rotation.
What is the difference between onPause and onStop?
onPause runs when the activity loses focus but may still be partly visible (a dialog or multi-window). onStop runs when it is completely hidden.
Is onDestroy always called?
No. If Android kills the process to free memory, onDestroy may not run. Save important data earlier, for example in onStop or immediately when the user changes it.
