https://kotlinlang.org logo
#dagger
Title
# dagger
r

rkeazor

04/02/2024, 11:19 PM
Hey does AssistedInject with Hilt support SharedViewModels, or will a new instance always be created?
n

Nicholas Doglio

04/02/2024, 11:24 PM
What does SharedViewModels mean in this context? Like scoping it to an Activity but then accessing it in Fragments?
r

rkeazor

04/02/2024, 11:56 PM
yea
h

Hristijan

04/03/2024, 7:51 AM
You can specify a key nowadays or if you want to scope it to a graph of your nested navigation you can use hiltNavGraphViewModels(R.id.myGraphId) also if you instantiate the view model in the activity, you can use the owner of the by viewModel to be the fragment’s activity lifecycle owner
❤️ 1
1
n

Nicholas Doglio

04/03/2024, 12:23 PM
To also answer your question more directly I would guess that the shared viewModel is created once lazily with whatever param you pass in via the Assisted injection in the Activity and all other calls to it would just grab the existing ViewModel but I think it would be worth testing to confirm that.
❤️ 1
2 Views