ScreenModel
The ScreenModel and rememberScreenModel are part of the core library.
ScreenModel is just like a ViewModel: designed to store and manage UI-related data in a lifecycle conscious way. It also allows data to survive configuration changes such as screen rotations.
Unlike ViewModel, ScreenModel is just an interface. It's also Android independent and doesn't requires an Activity or Fragment to work.
1
class HomeScreenModel : ScreenModel {
2
3
// Optional
4
override fun onDispose() {
5
// ...
6
}
7
}
Copied!
ScreenModel is integrated with Coroutines, RxJava, LiveData, Koin, Kodein and Hit!
By design, it's only possible to create a ScreenModel instance inside a Screen. Call rememberScreenModel and provide a factory lambda.
1
class HomeScreen : Screen {
2
3
@Composable
4
override fun Content() {
5
val screenModel = rememberScreenModel { HomeScreenModel() }
6
// ...
7
}
8
}
Copied!
If you need to have multiple instances of the same ScreenModel for the same Screen, add a tag to differentiate them.
1
val screenModel = rememberScreenModel(tag = "CUSTOM_TAG") { HomeScreenModel() }
Copied!

Sample

Source code here.
Last modified 1mo ago
Copy link
Contents
Sample