Lifecycle & Application Data
This content is for v1.1.2. Switch to the latest version for up-to-date documentation.
When developing mobile apps, you must distinguish between in-memory application state and durable local storage.
In-Memory State vs. Durable Storage
Section titled “In-Memory State vs. Durable Storage”StoreState is In-Memory: Data inside an in-memory@storeexists only as long as your Python process runs in Android RAM. If Android reclaims process memory in the background or the user swipes away the task, in-memory state resets on the next launch.- Durable Storage: For records that must survive app termination and updates (e.g. user authentication tokens, downloaded records, preferences), write them to local files or a local database.
Keep User Data Outside Managed Application Directories
Keep user data outside the managed application source directory, whose contents can be overwritten during app updates. Always test save/load behavior after a normal restart and after installing an update signed with the same key.
1. JSON Configuration & Preferences
Section titled “1. JSON Configuration & Preferences”import jsonimport os
DATA_FILE = "user_preferences.json"
def save_preferences(data: dict): with open(DATA_FILE, "w", encoding="utf-8") as f: json.dump(data, f)
def load_preferences() -> dict: if os.path.exists(DATA_FILE): with open(DATA_FILE, "r", encoding="utf-8") as f: return json.load(f) return {"dark_mode": True, "notifications": True}2. Local Relational Data: SQLite
Section titled “2. Local Relational Data: SQLite”Python’s built-in sqlite3 module works natively on Android without any external drivers:
import sqlite3
def init_db(): conn = sqlite3.connect("app_data.db") cursor = conn.cursor() cursor.execute(""" CREATE TABLE IF NOT EXISTS notes ( id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT, content TEXT ) """) conn.commit() conn.close()Android Lifecycle & Surface Stabilization
Section titled “Android Lifecycle & Surface Stabilization”When the user navigates away from your app:
- Android pauses and backgrounds the activity.
- In-memory state remains in RAM unless low memory forces process termination.
- Upon returning, Android resumes the activity.
Best Practices for Lifecycle Safety
Section titled “Best Practices for Lifecycle Safety”- Avoid irreversible side-effects on import: Do not create database records or launch background jobs solely because a module was imported or
viewwas evaluated. Development reloads and process restarts can execute those lines repeatedly. - Test realistic transitions: Verify behavior during device rotation, opening/closing the keyboard, switching to another app, and returning after a delay.
