Activity lifecycle methods help your app know what to do when the screen opens, closes, or changes. They keep your app working smoothly and save resources.
Activity lifecycle methods (onCreate, onStart, onResume, onPause, onStop, onDestroy) in Android Kotlin
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Initialize your activity here
}
override fun onStart() {
super.onStart()
// Activity is becoming visible
}
override fun onResume() {
super.onResume()
// Activity starts interacting with the user
}
override fun onPause() {
super.onPause()
// Activity is partially visible, pause ongoing actions
}
override fun onStop() {
super.onStop()
// Activity is no longer visible
}
override fun onDestroy() {
super.onDestroy()
// Clean up resources before activity is destroyed
}Each method calls super to keep the system working properly.
Methods run in a specific order as the activity moves through states.
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}override fun onPause() {
super.onPause()
// Pause music or animations here
}override fun onDestroy() {
super.onDestroy()
// Release resources like database or sensors
}This example logs messages to the console each time a lifecycle method runs. You can watch these logs to understand when each method is called as you open, use, and close the app.
import android.os.Bundle import android.util.Log import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) Log.d("Lifecycle", "onCreate called") } override fun onStart() { super.onStart() Log.d("Lifecycle", "onStart called") } override fun onResume() { super.onResume() Log.d("Lifecycle", "onResume called") } override fun onPause() { super.onPause() Log.d("Lifecycle", "onPause called") } override fun onStop() { super.onStop() Log.d("Lifecycle", "onStop called") } override fun onDestroy() { super.onDestroy() Log.d("Lifecycle", "onDestroy called") } }
Use onCreate to set up your UI and initialize variables.
onPause and onStop are good places to save data or pause tasks.
onDestroy is the last chance to clean up before the activity is removed.
Activity lifecycle methods let your app respond to changes in screen visibility and user interaction.
Always call super in each lifecycle method to keep the system stable.
Use these methods to manage resources, save data, and keep your app smooth and efficient.
Practice
onCreate(), onStart(), onResume(), or onPause()?Solution
Step 1: Understand activity launch sequence
When an activity starts,onCreate()is the first method called to set up the UI and initialize components.Step 2: Confirm order of lifecycle methods
AfteronCreate(),onStart()andonResume()follow, but they are not first.Final Answer:
onCreate() -> Option CQuick Check:
First lifecycle method = onCreate() [OK]
- Confusing onStart() as the first method
- Thinking onResume() is called before onCreate()
- Assuming onPause() is called on launch
onPause() method in an Android activity?Solution
Step 1: Check Kotlin override syntax
In Kotlin, to override a method, useoverride fun methodName()and callsuper.methodName()inside.Step 2: Validate each option
override fun onPause() { super.onPause() } correctly usesoverride fun onPause()and callssuper.onPause(). Others miss override keyword, have wrong syntax, or wrong super call.Final Answer:
override fun onPause() { super.onPause() } -> Option DQuick Check:
Correct override syntax = override fun onPause() { super.onPause() } [OK]
- Omitting the override keyword
- Calling super incorrectly
- Wrong function declaration syntax
override fun onPause() {
super.onPause()
println("Paused")
}
override fun onStop() {
super.onStop()
println("Stopped")
}What will be printed if the user presses the home button while the activity is visible?
Solution
Step 1: Understand lifecycle on home press
Pressing home triggersonPause()first, thenonStop()as activity goes to background.Step 2: Check print order
SinceonPause()runs beforeonStop(), "Paused" prints first, then "Stopped".Final Answer:
Paused Stopped -> Option BQuick Check:
onPause() then onStop() prints = Paused, Stopped [OK]
- Assuming onStop() runs before onPause()
- Thinking only one method runs on home press
- Confusing print order
override fun onPause() {
saveData()
}What is the main problem with this code?
Solution
Step 1: Check lifecycle method override rules
When overriding lifecycle methods, always callsuper.method()to keep system stable.Step 2: Identify missing super call
The code lackssuper.onPause(), which can cause unexpected behavior.Final Answer:
Missing call to super.onPause() -> Option AQuick Check:
Always call super in lifecycle overrides [OK]
- Forgetting to call super method
- Thinking onPause() can't be overridden
- Assuming saveData() timing is wrong
A)
onDestroy()B)
onStop()C)
onPause()D)
onResume()Solution
Step 1: Understand visibility and resource management
When activity is partially or fully hidden,onPause()is called first, signaling to release resources like camera to save battery.Step 2: Compare lifecycle methods for releasing resources
onStop()is later, but releasing inonPause()ensures resources are freed as soon as activity is not in foreground.onDestroy()may never be called if system kills process.onResume()is for acquiring resources.Final Answer:
onPause() -> Option AQuick Check:
Release resources in onPause() to save battery [OK]
- Waiting until onStop() or onDestroy() to release resources
- Releasing resources in onResume() by mistake
- Not releasing resources early enough
Ready to prove it?
Pass the test and this pattern moves unknown
