question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

sharedViewmodel with parameters reuses other instances

See original GitHub issue

Describe 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:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
StenSoftcommented, Sep 23, 2020

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.

1reaction
Swordsman-Inactioncommented, Dec 19, 2019

For this scenario, I think it’s better to use a setter like setId(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 a setter makes more sense than injection.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found