How to sharing viewmodel between an activity and each navgraph destination #1436
Answered
by
mikucat0309
mikucat0309
asked this question in
Q&A
Replies: 2 comments 3 replies
-
@Composable
inline fun <reified T : ViewModel> getActivityViewModel(): T =
getViewModel(owner = LocalContext.current as ComponentActivity) |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
mikucat0309
-
One of the best alternative to tackle this problem is to use dagger hilt. Which is splendid, as hiltViewModel would take care of this problem. It doesn't matter whether it's a navgraph or in some composables. I'll add the dependencies and a sample code below. Add this in the plugin in gradle
Add this as a dependency
Add this to your classpath
Once the you've successfully added this to your gradle files. Then in your composables use these
now the viewModel is directly injected to your composable. I hope this helps you. Happy coding!! |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I already read this document, but it only talks about sharing between destination, in other word, how to get same viewmodel under same acitivity, no matter whether in composable function or navgraph destination.
BTW,
koinNavGraphViewModel
can not work with Jetpack Compose, since it doesn't need fragments.Beta Was this translation helpful? Give feedback.
All reactions