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
 +-----------+     +-----------+
CallbackMomentTypical Job
onCreateFirst creationSet the layout, set up views
onStartScreen turns visibleStart light updates
onResumeScreen gets focusStart camera, animations
onPauseFocus fadesPause animations, save small drafts
onStopScreen hiddenRelease heavy resources
onRestartReturn from hiddenRefresh stale data
onDestroyScreen removedFinal 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 onResume and stop them in onPause.
  • Save drafts in onPause, because the system may remove the app after onStop.
  • Keep every callback quick. Slow code delays the screen.

Common Mistakes

  • Forgetting the super call, which crashes the app.
  • Starting a task in onStart and never stopping it, which leaks battery.
  • Storing large images in a Bundle, which exceeds size limits.