Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🟢 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

onCreate()set layout, find views, set listenersonStart()activity becomes visibleonResume()in front — user can interact ✅ RUNNING← runningonPause()another screen/dialog on toponStop()completely hidden (Home pressed)← back: onRestart→onStartonDestroy()finished or rotated
From onStop, coming back calls onRestart → onStart → onResume

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 actionCallbacks
Opens apponCreate → onStart → onResume
Presses Home / switches apponPause → onStop
ReturnsonRestart → onStart → onResume
Presses Back (leaves screen)onPause → onStop → onDestroy
Rotates phone / changes dark modefull destroy + create
Dialog / permission popup appearsonPause 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" }
}
count = 5onSaveInstanceStateputInt("count", 5)rotatedestroy + createonCreate(saved)getInt → 5
The Bundle survives recreation

For bigger data (lists from a database) use a ViewModel — you will learn it in the MVVM lesson.

What to do in which callback

CallbackDo this
onCreatesetContentView, binding, adapters, click listeners, observe ViewModel
onStart / onStopStart/stop things that matter while visible (location updates, animations)
onResume / onPauseCamera preview, sensors, video playback
onDestroyRelease resources not tied to lifecycle-aware components

Common mistakes

  • Forgetting super.onXxx() → SuperNotCalledException crash.
  • 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.
← Android Tutorial (Kotlin)
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

Next topicLogcat, Debugging & Emulator Your progress: 7 / 43 (16%) · Lesson 8 / 43 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free