Am Using Koin as Dependency injection pattern in my project, I need to create new instances whenever i load fragment/activity, now am using the following pattern, Any solution for that it might save lots of time.
private val homeViewModel: HomeViewModel by viewModel()
The question is why you want new instances everytime? The whole concept of ViewModel
is to retain the same instance and data. viewModel {}
creates new instance everytime you inject it unless it is not shared.
Don't know why it is not working for you, but I think you can use factory{}
instead of viewModel{}
.
factory{
// this is because you need new instance everytime.
HomeViewModel()
}
You are going to want to forego using by viewmodel
and instantiate the class directly. You can get global (scoped) variables through getKoin().get()
.
private val viewModel = HomeViewModel(getKoin().get())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With