Skip to content

Android vs Kivy Lifecycle

A lifecycle is the sequence of stages each Activity on your app transitions from when it starts to when it stops. For example, being created, paused, resumed, stopped and destroyed.


Android Lifecycle

In Android, the lifecycle of an Activity is managed by the Android system, which calls specific methods at different stages of the Activity's lifecycle.

flowchart TD
    A["onCreate()"] --> B["onStart()"]
    B --> C["onResume()"]
    C -- "App interrupted" --> D["onPause()"]
    D -- "User returns" --> C
    D -- "App fully hidden" --> E["onStop()"]
    E -- "User returns" --> F["onRestart()"]
    F --> B
    E -- "App closed" --> G["onDestroy()"]

    %% Professional color styling for clarity and contrast
    classDef create fill:#42a5f5,color:#fff,stroke:#1565c0,stroke-width:2px
    classDef running fill:#26a69a,color:#fff,stroke:#00796b,stroke-width:2px
    classDef pause fill:#ffe082,color:#222,stroke:#ffa000,stroke-width:2px
    classDef stop fill:#ffb74d,color:#222,stroke:#ef6c00,stroke-width:2px
    classDef destroy fill:#ef5350,color:#fff,stroke:#b71c1c,stroke-width:2px

    class A create
    class B,F running
    class C running
    class D pause
    class E stop
    class G destroy

Each stage lets you run arbitrary code to manage things like resources, saving data, or updating the interface. Both Android and Kivy define their own lifecycles.

What is an Activity?

An Activity is a space where users interact with your application, typically through a screen that displays content and allows user input. In Android, "an Activity is a single, focused thing that the user can do" [1].

But it is not just a screen; it is a component that provides the window in which you can place your UI. Depending on the context, it can appear fullscreen, as a floating window, or within a multi-window setup.

What matter for us here is that each Activity manages its own state and lifecycle, controlling how it transitions between being visible, active, paused, or stopped.

Android Lifecycle Stages and Methods

1. onCreate()

  • Called when the Activity is first created.
  • Used to initialize variables, set up the UI, and start essential processes.
  • The user does not see the screen yet.

2. onStart()

  • Called after onCreate.
  • Activity becomes visible, but the user cannot interact with it.

3. onResume()

  • Called after onStart.
  • Activity is in the foreground, fully interactive.
  • It stays in this state until interrupted by the system (e.g., incoming call, screen turning off) or the user leaves it (e.g., by pressing the home button or launching another app).

4. onPause()

  • Called when another Activity or UI component covers part or all of the Activity.
  • The Activity's resources stay in memory, as the user might come back.
  • Ideal place to save important data, because Android does not guarantee onStop or onDestroy will always be called.

5. onStop()

  • Called when the Activity is no longer visible to the user. This may happen either because a new activity is being started on top, an existing one is being brought in front of this one, or this one is being destroyed.
  • Typically occurs when navigating to a new Activity.
  • Unlike onPause(), where the Activity may still be partially visible, onStop() means it’s completely hidden.

6. onRestart()

  • Called when the Activity comes back to the foreground after being stopped (i.e., after onStop()).
  • It reinitializes elements before the Activity becomes visible again and then it calls onStart().

7. onDestroy()

  • Called when the Activity is finishing or destroyed by the system.
  • All resources are released.
  • Can occur if:
    1. The Activity calls finish()
    2. User presses the back button to close the Activity
    3. System needs memory
    4. A configuration change (like screen rotation) causes the Activity to be destroyed and recreated

Kivy Lifecycle

Kivy has its own lifecycle, which is different from Android's. Kivy applications are structured around the App class, which manages the application's lifecycle.

Kivy Lifecycle Stages and Methods

flowchart TD
    A["build()"] --> B["on_start()"]
    B --> C["App running"]
    C -- "App interrupted" --> D["on_pause()"]
    D -- "Resume" --> E["on_resume()"]
    E --> C
    D -- "Exit/Stop" --> F["on_stop()"]
    F --> G["App destroyed"]

    %% Color styles for dark mode readability
    classDef start fill:#26a69a,color:#fff,stroke:#00796b,stroke-width:2px
    classDef running fill:#42a5f5,color:#fff,stroke:#1565c0,stroke-width:2px
    classDef pause fill:#ffe082,color:#222,stroke:#ffa000,stroke-width:2px
    classDef stop fill:#ffb74d,color:#222,stroke:#ef6c00,stroke-width:2px
    classDef final fill:#ef5350,color:#fff,stroke:#b71c1c,stroke-width:2px

    class A,B start
    class C,E running
    class D pause
    class F stop
    class G final

The Kivy lifecycle methods are called in the following order:

0. build()

Called when the app instance is created, before the app window appears. Should return the root widget of your application.

Avoid any operations unrelated to building the UI; this method should focus solely on initializing the widget structure. In practice, within build() you should only:

  • Create and return widgets
  • Set widget properties
  • Define the widget tree/layout

1. on_start()

Called once when the application is starting, after the window is created and the UI is ready. Use to start timers, schedule tasks, open resources, or trigger initial data loads or:

  • Access or modify files
  • Start background threads or async tasks
  • Open network connections
  • Read/write from/to databases
  • Change app settings
  • Play sounds or animations
  • Schedule Clock events
  • Dispatch events
  • Log in users or make API calls

2. on_pause()

Called when the application loses focus, such as when the user switches to another app, receives a call or the screen turns off.

Use to save application state, pause ongoing operations, and release exclusive resources (like the camera).

On Android, must return True to allow the app to be paused and resumed later. If it returns False, the app will be stopped instead of paused, and on_stop() will be called.

3. on_resume()

  • Called when the application regains focus after being paused.
  • Use to restore state, restart paused tasks, and reacquire resources

4. on_stop()

Called when the application is stopping (user closes the app or system kills the process). Your last chance to clean up resources, save data, or perform final tasks.