Kotlin Activity Lifecycle
An Activity is one screen of an Android app. A screen does not live forever. The system creates it, shows it, hides it, and destroys it as the user moves around. The lifecycle describes those stages. Callback functions let your code react at each stage.
A Shop Through the Day
A shop opens in the morning, serves customers, pauses during lunch, closes at night, and sometimes rearranges shelves. A good shopkeeper performs the right task at each moment: unlock the door, switch on lights, count the cash, lock up. Activity callbacks give you the same checkpoints.
The Seven Callbacks
+-----------+
| onCreate | screen is built
+-----------+
|
+-----------+
+----->| onStart | screen becomes visible
| +-----------+
| |
| +-----------+
| +-->| onResume | user can interact (RUNNING)
| | +-----------+
| | | another screen covers part
| | +-----------+
| +---| onPause | losing focus
| +-----------+
| | screen fully hidden
| +-----------+
| | onStop | not visible
| +-----------+
| | |
+-----------+ | +-----------+
| onRestart |-+ | onDestroy | screen is removed
+-----------+ +-----------+
| Callback | Moment | Typical Job |
|---|---|---|
| onCreate | First creation | Set the layout, set up views |
| onStart | Screen turns visible | Start light updates |
| onResume | Screen gets focus | Start camera, animations |
| onPause | Focus fades | Pause animations, save small drafts |
| onStop | Screen hidden | Release heavy resources |
| onRestart | Return from hidden | Refresh stale data |
| onDestroy | Screen removed | Final cleanup |
Watching the Lifecycle
Log messages reveal the order. Override each callback and call the parent version first.
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Log.d("Lifecycle", "onCreate")
}
override fun onStart() {
super.onStart()
Log.d("Lifecycle", "onStart")
}
override fun onResume() {
super.onResume()
Log.d("Lifecycle", "onResume")
}
override fun onPause() {
super.onPause()
Log.d("Lifecycle", "onPause")
}
override fun onStop() {
super.onStop()
Log.d("Lifecycle", "onStop")
}
override fun onDestroy() {
super.onDestroy()
Log.d("Lifecycle", "onDestroy")
}
}Common Sequences
Opening the App
onCreate - onStart - onResume
Pressing Home
onPause - onStop
Returning from Home
onRestart - onStart - onResume
Pressing Back to Exit
onPause - onStop - onDestroy
Rotating the Phone
onPause - onStop - onDestroy - onCreate - onStart - onResume
Rotation destroys the screen and builds a new one. Every variable inside the Activity resets unless you protect the data.
Keeping Data Across Rotation
The function onSaveInstanceState stores small values in a Bundle. The new screen receives that Bundle inside onCreate.
private var score = 0
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putInt("score", score)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
score = savedInstanceState?.getInt("score") ?: 0
}A ViewModel survives rotation and suits larger data. A Bundle suits small values that must survive even when the system kills the app process.
Choosing the Right Callback
- Build views and read intent data in
onCreate. - Start the camera or sensors in
onResumeand stop them inonPause. - Save drafts in
onPause, because the system may remove the app afteronStop. - Keep every callback quick. Slow code delays the screen.
Common Mistakes
- Forgetting the
supercall, which crashes the app. - Starting a task in
onStartand never stopping it, which leaks battery. - Storing large images in a Bundle, which exceeds size limits.
