sharedViewmodel with parameters reuses other instances
See original GitHub issueDescribe the bug When using sharedViewModel that takes a parameter and injecting another instance with a different parameter the old instance is getting reused.
To Reproduce
Module definition:
viewModel { (id: Int) -> UserViewModel(id) }
Fragment A/B
val details: UserViewModel by sharedViewModel { parametersOf(1) }
Fragment C/D
val details: UserViewModel by sharedViewModel { parametersOf(2) }
Launching A creates a new Instance with the expected parameter 1, launching B resues that instance. Thats fine so far. However: Launching C reuses the same instance and the id is not changed as expected. same for D.
Expected behavior A new instance should be created when the parameters are different
Koin project used and used version (please complete the following information):
koin-androidx-viewmodel version 2.0.1
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:6 (1 by maintainers)
Top Results From Across the Web
How to use a shared ViewModel, and avoid reusing the same ...
You have the same instance of a shared ViewModel , because it belongs to the Activity - that's clear. I don't know exactly...
Read more >App Failures by Koin: Story 3 — Issue With sharedViewModel()
Fragments are reusing the shared ViewModel with by sharedViewModel() . If you're using parameterless constructors of ViewModels in your app you ...
Read more >Shared ViewModel Across Fragments - Android Developers
You will learn how to use a shared ViewModel to share data between the fragments of the same activity and new concepts like...
Read more >Android ViewModel & Navigation | Koin
One ViewModel instance can be shared between Fragments and their host Activity. To inject a shared ViewModel in a Fragment use: by activityViewModel()...
Read more >Can the same instance of a fragment be reused with a ... - Reddit
where we have the same fragment class and provide to it different arguments. Can the same instance be "recycled" and reused with different...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I think it makes sense to create multiple instances with different arguments. E.g. I have a product detail fragment with another fragment showing more detail, it makes sense to have one view model for each product (with different arguments), just as there are multiple fragment instances created by Android Navigation Component for different arguments, and to share this view model between the detail fragment and the more detail fragment.
For this scenario, I think it’s better to use a
setter
likesetId(id: String)
instead of parameter injection.Injection normally happens when create the dependency object, since
UserViewModel
is a shared object, it will only be created once, so the parameter will be only injected once.If
id
can be changed, then asetter
makes more sense than injection.