Activities & Lifecycle
Learn what an Activity is, understand the Activity lifecycle callbacks (onCreate, onStart, onResume, onPause, onStop, onDestroy), and use ViewBinding.
Activities & Lifecycle is a free Android Academy lesson on CoddyKit. This is lesson 2 of 7. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Android Academy learning path, and your progress syncs across the web and the CoddyKit app. The Android Academy course includes 7 lessons in total.
What Is an Activity?
An Activity is a single screen in your Android app.
- Every screen (login, home, settings) is an Activity
- It manages a layout (XML) and user interactions
- Declared in AndroidManifest.xml
Think of it as the Android equivalent of a web page — each page is an Activity.
Activity Lifecycle
An Activity goes through a series of lifecycle states as the user navigates the app:
- Created → Started → Resumed (visible & active)
- Paused (partially hidden)
- Stopped (completely hidden)
- Destroyed (Activity is gone)
You override lifecycle methods to respond to these transitions.
Lifecycle Callbacks
The six main lifecycle callbacks:
onCreate()— Activity is being created. Set up your layout here.onStart()— Activity becomes visible.onResume()— Activity is in the foreground, user can interact.onPause()— User is leaving. Save temporary data.onStop()— Activity is no longer visible.onDestroy()— Activity is being destroyed.
MainActivity Example
A minimal Activity with lifecycle logging:
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
private val TAG = "MainActivity"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Log.d(TAG, "onCreate")
}
override fun onResume() {
super.onResume()
Log.d(TAG, "onResume — user is here")
}
override fun onPause() {
super.onPause()
Log.d(TAG, "onPause — saving state")
}
override fun onDestroy() {
super.onDestroy()
Log.d(TAG, "onDestroy")
}
}Why Lifecycle Matters
Common mistakes caused by ignoring the lifecycle:
- Playing a video in
onResumebut forgetting to pause it inonPause— drains battery - Registering a sensor listener in
onCreatebut not unregistering inonDestroy— memory leak - Not saving form data in
onPause— user loses progress on a phone call
Saving State: onSaveInstanceState
When the user rotates the phone, the Activity is destroyed and recreated. Your UI state is lost.
Save transient state in onSaveInstanceState(bundle) and restore it in onCreate(savedInstanceState).
Saving & Restoring State
Persist data across configuration changes:
class CounterActivity : AppCompatActivity() {
private var count = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_counter)
// Restore saved state after rotation
count = savedInstanceState?.getInt("count") ?: 0
}
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putInt("count", count)
}
}Back Stack
Android manages a back stack of Activities:
- Launching a new Activity pushes it onto the stack
- Pressing Back pops the top Activity and destroys it
- The previous Activity resumes automatically
This is why you shouldn't call finish() manually unless you specifically want to remove the current screen from the back stack.
Configuration Changes
Any configuration change (rotation, language change, keyboard attached) recreates the Activity by default.
The modern solution is to use a ViewModel — it survives configuration changes and holds your data safely. You'll learn ViewModel in the Architecture module.
Quick Check
In which lifecycle method should you set up the layout (call setContentView)?
Recap: Activities & Lifecycle
You now understand Android Activities:
- An Activity = one screen in your app
- Lifecycle: onCreate → onStart → onResume → onPause → onStop → onDestroy
- Save transient state in
onSaveInstanceState - Rotation destroys and recreates the Activity
- Android manages a back stack automatically
Next: designing screens with layouts and views.
Learn Kotlin with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 36
- Lessons
- 152
Frequently Asked Questions
Is the “Activities & Lifecycle” lesson free?
Yes — any 3 lessons of the Android Academy learning path, including “Activities & Lifecycle”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Android Academy course includes 7 lessons in total.
What will I learn in “Activities & Lifecycle”?
Learn what an Activity is, understand the Activity lifecycle callbacks (onCreate, onStart, onResume, onPause, onStop, onDestroy), and use ViewBinding. You practise Android Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Android Academy?
No prior experience is required. Android Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 7.
How long does the “Activities & Lifecycle” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Android Academy lesson?
Yes. Every Android Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Project Structure & Manifest
- Activities & Lifecycle
- Layouts & Views
- Handling User Input
- Intents & Navigation
- Fragments
- Permissions & Runtime Requests