I have nested fragments and then Using `ktx` funct...
# android
k
I have nested fragments and then Using
ktx
function
by viewModels
how can i get parent Fragment's
ViewModel
? I have tried this
Copy code
private val homeViewModel by parentFragment!!.viewModels<HomeViewModel>()
but it did not worked
t
Try with
private val homeViewModel by viewModels<HomeViewModel>(::requireParentFragment)
. Your fragment is probably not attached to its parent at the time it is created, so your code will fail with KotlinNPE. You have to specify the
ownerProducer
parameter so the parent fragmenr can be retrieved lazily.