Android - by viewModels() with injectable constructor on ViewModel
Asked Answered
A

1

7

Was wondering how someone would deal with this.

I have a fragment that has a respective view model. That view model has an injected repo in its constructor. However when using "by viewModels()" to create the view model instance in my fragment I'm getting an error.

Example:


@Singleton
class MyViewModel @Inject constructor(val someRepo: SomeRepo) : ViewModel() { ... }

class MyFragment : BaseFragment(), Injectable {
    val myViewModel: MyViewModel by viewModels()
    ...
}
Error:     java.lang.RuntimeException: Cannot create an instance of class com.example.MVVM.ViewModel.MyViewModel

Has anyone got this to work without creating their own viewModelFactory?

Ariadne answered 3/12, 2019 at 12:48 Comment(6)
You don't want to make the MyViewModel singleton, your onCleared() will not work as expected.Sheik
1 2 3Dalessandro
@Sheik Good call on the singleton, not sure why I added that.Ariadne
@sonnet ty for the linksAriadne
@Sheik Please reason for You don't want to make the MyViewModel singleton, your onCleared() will not work as expected.Windsucking
@VikashKuwarTiwari ViewModel has a lifecycle ensured by the ViewModelStoreOwner. Using Singleton will break this contract.Sheik
I
7

You need to do several things in order to inject stuff into viewmodel:

  1. Having custom ViewModelFactory which would be part of your graph
  2. Bind your Viewmodel class into the graph
  3. Inject this factory to your Fragment
  4. Use custom factory in the viewModels method by viewModels { theInjectedFactory}

All steps 1-3 are described in many articles or answers on SO, check e.g:

PS: as EpicPandaForce mentioned, you shouldn't have your viewmodel marked with @Singleton

Illuminati answered 4/12, 2019 at 11:0 Comment(1)
For those who wants to inject the viewmodel in activity the process is same. 3. Inject this factory to your activity 4. Use custom factory in the viewModel method by viewModels { theInjectedFactory }Milomilon

© 2022 - 2024 — McMap. All rights reserved.