Hilt integration
To use the getScreenModel you should first import cafe.adriel.voyager:voyager-hilt (see Setup).

@Inject

Add @Inject annotation to your ScreenModel.
1
class HomeScreenModel @Inject constructor() : ScreenModel {
2
// ...
3
}
Copied!
Call getScreenModel() to get a new instance.
1
class HomeScreen : AndroidScreen() {
2
3
@Composable
4
override fun Content() {
5
val screenModel = getScreenModel<HomeScreenModel>()
6
// ...
7
}
8
}
Copied!

@AssistedInject

Add @AssistedInject annotation to your ScreenModel and provide a ScreenModelFactory annotated with @AssistedFactory.
1
class PostDetailsScreenModel @AssistedInject constructor(
2
@Assisted val postId: Long
3
) : ScreenModel {
4
5
@AssistedFactory
6
interface Factory : ScreenModelFactory {
7
fun create(postId: Long): PostDetailsScreenModel
8
}
9
}
Copied!
Call getScreenModel() and use your factory to create a new instance.
1
data class PostDetailsScreen(val postId: Long): AndroidScreen() {
2
3
@Composable
4
override fun Content() {
5
val screenModel = getScreenModel<PostDetailsScreenModel, PostDetailsScreenModel.Factory> { factory ->
6
factory.create(postId)
7
}
8
// ...
9
}
10
}
Copied!

Sample

Sample code here.
Last modified 3mo ago