Skip to content
BENIBIX Learn

App Lifecycle

Development📱 Mobile DevelopmentMobile Features

Also known as App Life Cycle, Mobile App Lifecycle, Activity Lifecycle, App States, Scene Lifecycle

The states an app moves through, from open on screen to ended by the phone.

Think of it like

Think of it like an actor in a play. They are on stage, then wait at the side, then go home.

Example

Emma writes a long message in her chat app and switches to the camera. The phone ends the chat app to free memory. Because the app saved the draft as it left the screen, the message is still there when she comes back.

Why it matters

A phone has little memory and a battery to protect. Apps that save at the right moment never lose a person's work.

Definition

The app lifecycle is the set of states a phone app moves through. It starts when you tap the icon. It is in the foreground while you use it. When you leave, it goes to the background and soon stops running. The phone can then end it to free memory, with no warning. Each change calls a function in your code.

Example code

kotlin
override fun onStop() {
    super.onStop()
    saveDraft()  // the screen is gone, so save now
}
On Android, onStop runs when the app leaves the screen. It is a safe place to save.
The last call an app can count on comes as it leaves the screen, so that is where it saves.

Common mistake

Saving only when the app closes. The phone can end a suspended app with no last call, so save when it leaves the screen.

Key takeaways

  1. 1
    Foreground. An app on screen gets the chip and full speed.
  2. 2
    Background. Off screen, an app gets a moment to save, then stops running.
  3. 3
    Ended. The phone ends a suspended app when memory runs low, with no warning.

Compare