https://kotlinlang.org logo
#compose
Title
# compose
j

Jasmin Fajkic

07/19/2022, 9:15 AM
If I use hilt viewmodel in two composables on same route ( same screen ) will it be same object or it will create two different objects? Sorry little bit newbie question 😞
z

Zun

07/19/2022, 12:12 PM
Depends. Where are you creating the ViewModel? If you create it once then pass it to the two different Composables as parameter then obviously it will use the same ViewModel instance
j

Jasmin Fajkic

07/19/2022, 12:13 PM
Nah it works fine.
hiltViewModel
is already gonna return the same object from any composable within a single context, e.g. activity, fragment or a navigation route.
z

Zun

07/19/2022, 12:14 PM
Depends how you scope it, you can easily create two different instances of a ViewModel even in the same screen
j

Jasmin Fajkic

07/19/2022, 12:15 PM
Nah I need same instance 😄
I know that I can with different keys right?
59 Views