The lifecycle of a screen

The seven lifecycle methods of an Android activity (onCreate, onStart, onResume, onPause, onStop, onDestroy, onRestart), the events that trigger them (opening, going home, rotation, incoming call, process death) and saving state.

  • 27 cards
  • 6 topics
  • Free

The outline, topic by topic

Each topic is weighted the way the exam weighs it.

  1. Concepts
  2. Questions
  3. Sort and link
  4. The steps
  5. Scenarios
  6. Understand

A few cards from the deck

  • Android activity: what is it?

    Show the answer

    The component that represents one screen of the app and whose methods the system calls at each change of state.

    The app does not decide alone when a screen appears or disappears: the system notifies it through the lifecycle methods, which you override.

  • Each method below writes its name to Logcat. The user opens the screen, presses Home, then reopens the app. What is the output?

    ```kotlin
    class MainActivity : AppCompatActivity() {
    override fun onCreate(state: Bundle?) { super.onCreate(state); Log.d("Cycle", "onCreate") }
    override fun onStart() { super.onStart(); Log.d("Cycle", "onStart") }
    override fun onResume() { super.onResume(); Log.d("Cycle", "onResume") }
    override fun onPause() { super.onPause(); Log.d("Cycle", "onPause") }
    override fun onStop() { super.onStop(); Log.d("Cycle", "onStop") }
    override fun onRestart() { super.onRestart(); Log.d("Cycle", "onRestart") }
    override fun onDestroy() { super.onDestroy(); Log.d("Cycle", "onDestroy") }
    }
    `

    • onCreate, onStart, onResume, onPause, onStop, onRestart, onStart, onResume
    • onCreate, onStart, onResume, onPause, onStop, onDestroy, onCreate, onStart, onResume
    • onCreate, onStart, onResume, onPause, onStop, onStart, onResume
    • onCreate, onStart, onResume, onPause, onResume
    Show the answer

    onCreate, onStart, onResume, onPause, onStop, onRestart, onStart, onResume

    Home sends the screen to the background without destroying it: onPause then onStop. On return the instance still exists, so onRestart, onStart, onResume; onCreate is not called again. The version with onDestroy is what a rotation does.

  • When the screen is created, the system calls onCreate, … then … in that order, after which the user can interact.

    Show the answer

    When the screen is created, the system calls onCreate, onStart then onResume in that order, after which the user can interact.

    onCreate sets the screen up once, onStart makes it visible, onResume brings it to the foreground, ready for gestures.

  • Activity lifecycle: what is it?

    Show the answer

    The series of states a screen goes through, from creation to destruction, with a method called at each transition.

    Create in onCreate, resume in onResume, release in onPause or onStop: each job has its method, otherwise memory, battery or a resource leaks.

  • An activity's life, from creation to destruction: which step comes after "onCreate"?

    • onResume
    • onStart
    • onPause
    • onStop
    Show the answer

    onStart

    The methods come in symmetric pairs: onCreate/onDestroy, onStart/onStop, onResume/onPause. What is opened in one is closed in its twin.