← Back to notebook index

Notebook Topic

App Lifecycle

English topic guide inspired by the original iOS Developer Notebook, expanded for both iOS and Android teams.

What this topic covers

Execution checklist

iOS implementation notes

Android implementation notes

Code examples

Swift

@main
struct MyApp: App {
  @Environment(\.scenePhase) private var scenePhase
  var body: some Scene {
    WindowGroup { RootView() }
      .onChange(of: scenePhase) { phase in
        if phase == .background { Task { await AppState.shared.persist() } }
      }
  }
}

Kotlin

class App : Application(), DefaultLifecycleObserver {
  override fun onCreate() {
    super.onCreate()
    ProcessLifecycleOwner.get().lifecycle.addObserver(this)
  }
  override fun onStop(owner: LifecycleOwner) { AppState.persist() }
}

Reference links

Related topics