New viewmodel for every instance of same fragment
See original GitHub issueDescribe the bug I have a fragment which has a personId to show details of a person
class PersonDetailsFragment : Fragment {
val personId: Int by lazy { intent.extras.getInt("personId") }
val viewModel: PersonViewModel by viewModel()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
viewModel.personDetailsLiveData.observe(this, Observer<List<Cryptocurrency>> {
// show person
})
}
}
To Reproduce Steps to reproduce the behavior:
- Open a person (PersonFragment)
- Click on a friend of this person
- It opens a new instance of PersonFragment but gets the viewmodel of the previous PersonFragment
- The app crashes with the following exception
java.lang.IllegalArgumentException: Cannot add the same observer with different lifecycles
Expected behavior If i open a new instance of the same fragment it should also create a new viewmodel.
Koin project used and used version (please complete the following information): koin-android-architecture v0.9.3
Issue Analytics
- State:
- Created 5 years ago
- Comments:16 (8 by maintainers)
Top Results From Across the Web
Android returning different instance of ViewModel for the same ...
A ViewModel is always created in association with a scope (an fragment or an activity) and will be retained as long as the...
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 >Communicate Between Fragments Using ViewModel - Medium
If the activity is re-created, it and all its fragments receive the same ViewModel instance that was created by the associated activity.
Read more >Shared ViewModel in Android - GeeksforGeeks
In android, we can use ViewModel to share data between various fragments or activities by sharing the same ViewModel among all the fragments...
Read more >Process kill and Activity kill break "previousBackStackEntry ...
Normally everything works fine, and both Fragments receive the same instance of ViewModel, and communicate properly.. until I trigger Process kill or ...
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
Hello,
you are injecting with
by inject()
.You have to use
by viewModel()
to inject and bind your viewModel to the lifecycle owner:val viewModel: PersonViewModel by viewModel()
@arnaudgiuliani I’m using version
2.0.0-beta-1
, and I’m facing the exact same issue as described above. I have declared my ViewModel classes withviewModel
dsl keyword.I’m also using Navigation Component to navigate between fragments. When I navigate from fragment A to B, and then navigate back to A, a new ViewModel is created and the observer is called multiple times.
Is there something that I’m doing wrong?