Since Activities/Fragments can be destroyed and re...
# android-architecture
t
Since Activities/Fragments can be destroyed and recreated using the same ViewModel, does it make sense for us to create scoped Dagger components in our ViewModels? Would there be anything wrong with doing it that way?
g
no, it’s completely valid and imo the best approach, to have own scope for ViewModel
👍 1
t
Thanks @gildor Do you inject in the
init {...}
block?
u
wouldnt that make testing ugly if you have globalInjector as the ctor parameter of the viewmodel?
t
ah, actually i’m still trying to understand the best way to handle testing injection. do you have a sample of how you inject your viewmodels?
g
Do you inject in the
init {...}
block?
No, just use constructor injection + factories
Agree with Vlastimil, using global injection in case of VM is not neccessary, better to use constructor injection, also no problem with testing
u
@gildor btw where do you keep the vm scoped component? it would possible make sens to have on on the vm instance in case of retained vms, like aac. but yet again, ugly, ctor injection would be nicer
t
@gildor btw where do you keep the vm scoped component?
This is my question as well