Kotlin Multiplatform Development 1.6.2 Help

Lifecycle

Lifecycle of components in Compose Multiplatform is adopted from the Jetpack Compose lifecycle concept. Lifecycle-aware components can react to changes in the lifecycle state of other components and help you produce better-organized, and often lighter, code that is easier to maintain.

Compose Multiplatform provides a common LifecycleOwner implementation, which extends the original Jetpack Compose functionality to other platforms and helps observe lifecycle states in common code.

States and events

The flow of lifecycle states and events (same as for the Jetpack lifecycle):

Lifecycle diagram

Mapping Android lifecycle to other platforms

iOS

Native events and notifications

Lifecycle event

Lifecycle state change

viewDidDisappear

ON_STOP

STARTEDCREATED

viewWillAppear

ON_START

CREATEDSTARTED

willResignActive

ON_PAUSE

RESUMEDSTARTED

didBecomeActive

ON_RESUME

STARTEDRESUMED

didEnterBackground

ON_STOP

STARTEDCREATED

willEnterForeground

ON_START

CREATEDSTARTED

Web

Due to limitations of the Wasm target, lifecycles:

  • Skip the CREATED state, as the application is always attached to the page.

  • Never reach the DESTROYED state, as web pages are usually terminated only when the user closes the tab.

Native event

Lifecycle event

Lifecycle state change

blur

ON_PAUSE

RESUMEDSTARTED

focus

ON_RESUME

STARTEDRESUMED

Desktop

Swing listener callbacks

Lifecycle event

Lifecycle state change

windowIconified

ON_STOP

STARTEDCREATED

windowDeiconified

ON_START

CREATEDSTARTED

windowLostFocus

ON_PAUSE

RESUMEDSTARTED

windowGainedFocus

ON_RESUME

STARTEDRESUMED

dispose

ON_DESTROY

CREATEDDESTROYED

Lifecycle implementation

Composables generally don't need unique lifecycles: a common LifecycleOwner usually provides a lifecycle for all interconnected entities. By default, all composables created by Compose Multiplatform share the same lifecycle: they can subscribe to its events, refer to the lifecycle state, and so on.

For details on how lifecycle works in navigation components, see Navigation and routing.

ViewModel implementation

The Android ViewModel approach to building UI can now be implemented in common code, with a couple of restrictions:

To use the multiplatform ViewModel implementation, add the following dependency to your commonMain source set:

kotlin { // ... sourceSets { // ... commonMain.dependencies { // ... implementation("org.jetbrains.androidx.lifecycle:lifecycle-viewmodel-compose:2.8.0-beta02") } // ... } }

Keep in mind the current limitations of the library:

  • Current ViewModel implementation is considered Experimental.

    That's why the ViewModelStoreOwner interface is currently implemented only in the scope of the navigation library. We plan to add a common implementation of the interface to Compose Multiplatform in future releases. However, you can implement it yourself for your specific project.

  • The ViewModel class works out of the box only for Android and desktop, where objects of the needed class can be created through class reflection. For iOS and web targets, you have to implement factories that explicitly create new ViewModel instances.

Last modified: 30 April 2024