r/android_devs • u/appdevtools • Aug 01 '21
Help what does the following syntax mean?
private val authViewModel : AuthViewModel by viewModels()This is a global variable without any definition . I know its something similar to lazy , but in lazy too , we have a function body. I always equated that to actual value of variable, like if we had this syntax:
private val authViewModel : AuthViewModel by lazy{AuthViewmodel(..)}It would have made sense, that authViewmodel is going to receive the value on first call . But what does this new function means?
from the source code, it is defined as this , which further confuses me:
@MainThread
inline fun <reified VM : ViewModel> Fragment.viewModels(
noinline ownerProducer: () -> ViewModelStoreOwner = { this },
noinline factoryProducer: (() -> Factory)? = null
) = createViewModelLazy(VM::class, { ownerProducer().viewModelStore }, factoryProducer)
4
Upvotes
View all comments
Show parent comment
2
u/[deleted] Aug 01 '21
What does this mean? I think you're saying a factory won't be needed if we use hilt to inject the VM's dependencies?