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 is becoming visible, but the user cannot interact with it.
3. onResume()¶
- Called after
onStart
. - Activity is in the foreground, fully visible, responsive and 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
oronDestroy
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 (receive a phone call, press home button, etc), 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:
- The Activity calls
finish()
- User presses the back button to close the Activity
- System needs memory
- A configuration change (like screen rotation) causes the Activity to be destroyed and recreated
- The Activity calls
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()"]:::start --> B["on_start()"]:::start
B --> C["App running"]:::running
C -- "App interrupted" --> D["on_pause()"]:::pause
D -- "Resume" --> E["on_resume()"]:::running
E --> C
D -- "OS may kill process" --> G["App destroyed"]:::final
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 final fill:#ef5350,color:#fff,stroke:#b71c1c,stroke-width:2px
class A,B start
class C,E running
class D pause
class G final
Color legend¶
flowchart LR
init_leg["Initialization"]:::build --> run_leg["Running"]:::running <--> pause_leg["Paused"]:::pause --> dest_leg["Destroyed"]:::final
classDef build 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
flowchart TB
build_desktop["build()"]:::build --> on_start_desktop["on_start()"]:::build
on_start_desktop --> running_desktop["App running"]:::running
running_desktop --> on_stop_desktop["on_stop()"]:::stop
on_stop_desktop --> destroyed_desktop["App destroyed"]:::final
classDef build fill:#26a69a,color:#fff,stroke:#00796b,stroke-width:2px
classDef running fill:#42a5f5,color:#fff,stroke:#1565c0,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
Color legend¶
flowchart LR
init_leg["Initialization"]:::build --> run_leg["Running"]:::running --> stop_leg["Stopping"]:::stop --> dest_leg["Destroyed"]:::final
classDef build 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
The Kivy lifecycle methods are called in the following order:
0. build()
¶
Called when the run
method is called on the App
instance.
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()
¶
Also called when the run
method is called on the App
instance. When the application is starting, after the window is created, but the UI is NOT visible and NOT ready to interact with the user yet.
Avoid heavy tasks here to prevent a black screen. Instead, schedule them with Clock.schedule_once
. Typical tasks to postpone with Clock.schedule_once
inside on_start()
include:
- 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
- Dispatch events
- Log in users or make API calls
2. on_pause()
¶
Android only, on desktop on_pause
is never called.
Called when the application loses focus, such as when the user switches to another app, receives a call or the screen turns off. Use this method to save the application's state, pause any ongoing operations, and release exclusive resources (e.g., the camera).
Important
If you want the app to be paused and resumed later, you must return True
from this method. Returning False
will stop the app instead of pausing it, and on_stop()
will be called instead. Never do that (your app will die every time it loses focus).
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 closing on desktop (this is not expected to be triggered on Android if on_pause
correctly returns True
). Typically occurs when the user closes the app. This is the final opportunity to release resources, save data, or perform any necessary cleanup.