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.

New viewmodel for every instance of same fragment

See original GitHub issue

Describe 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:

  1. Open a person (PersonFragment)
  2. Click on a friend of this person
  3. It opens a new instance of PersonFragment but gets the viewmodel of the previous PersonFragment
  4. 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:closed
  • Created 5 years ago
  • Comments:16 (8 by maintainers)

github_iconTop GitHub Comments

6reactions
arnaudgiulianicommented, Jun 11, 2018

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()

2reactions
joelfernandes19commented, Feb 19, 2019

@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 with viewModel 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?

Read more comments on GitHub >

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

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