ViewModel init call
See original GitHub issueHi, I get your sample code for ViewModel
class BackpackViewModel : ViewModel() {
val backpack: Backpack by inject()
}
and add an initialization block like here
class BackpackViewModel : ViewModel() {
val backpack: Backpack by inject()
init {
backpack.doSomething()
}
}
But I got this exception
java.lang.IllegalStateException: The dependency has not be injected yet.
If i call backpack.doSomething()
after init call everything is working.
How I fix it?
Issue Analytics
- State:
- Created 3 years ago
- Comments:6
Top Results From Across the Web
android - init is not called when injecting viewModel using Hilt
But there is one case that when I call a function from viewModel inside activity first init triggers, and then that function is...
Read more >How I can call a Init() function from my ViewModel?
Hi Markus, include call Init method in ctor of ViewModel. Your code ist a mixture of CodeBehind and MVVM. <Grid DataContext="{StaticResource vm} ...
Read more >ViewModel initialization - RainbowCake
This is best done by fetching the state in the ViewModel 's initializer block ( init ). This approach is useful if fetching...
Read more >Jetpack ViewModel initialization - Rock and Null
ViewModels in Android Jetpack is an awesome approach for decoupling business logic from Activities/Fragments. And is extremely easy to get ...
Read more >ViewModel overview - Android Developers
ViewModel lets you manage your UI's data in a lifecycle-aware fashion.
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 FreeTop 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
Top GitHub Comments
I think an example will be enough. thx.
@afaucogney is right (thx for the answer!), creation happens before injection. The process is divided into 2 steps:
installViewModelBinding
on yourscope
, we create theviewmodel
using the right APIs under the hood.KTP...inject()
inside your activity, it’s when we inject the dependencies of theviewmodel
.The issue is that you are trying to access to those dependencies on the first step, when they are not ready yet. This is similar to when you wanna access injected fields inside a constructor. If you are curious, I could explain more about the reasons why we designed it that way.
Nevertheless, there is a way to solve it: using a
ViewModel Factory
to create theviewmodel
using Toothpick.By doing so, you can use constructor and field injection within your
viewmodel
.Same, if you are curious, I can provide more info about why we don’t provide that functionality out-of-the-box.