KMP Tutorial #10: Shared ViewModel in Kotlin Multiplatform
On Android, ViewModels hold UI state and survive configuration changes. On iOS, ObservableObjects do the same job. Two platforms, two patterns, two codebases. But the logic is often identical. Loading a user list, handling errors, managing form state — the business logic does not change between platforms. Only the UI does. With KMP, you can write your ViewModel once in commonMain and use it on both Android and iOS. Since androidx.lifecycle:lifecycle-viewmodel version 2.8, ViewModel officially supports Kotlin Multiplatform. ...