https://kotlinlang.org logo
#android-architecture
Title
# android-architecture
t

tenprint

11/28/2018, 11:02 PM
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

gildor

11/29/2018, 6:41 AM
no, it’s completely valid and imo the best approach, to have own scope for ViewModel
👍 1
t

tenprint

11/29/2018, 2:49 PM
Thanks @gildor Do you inject in the
init {...}
block?
u

ursus

11/29/2018, 5:52 PM
wouldnt that make testing ugly if you have globalInjector as the ctor parameter of the viewmodel?
t

tenprint

11/29/2018, 7:02 PM
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

gildor

11/30/2018, 1:14 AM
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

ursus

11/30/2018, 2:05 AM
@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

tenprint

11/30/2018, 3:22 PM
@gildor btw where do you keep the vm scoped component?
This is my question as well
2 Views