Kulwinder Singh
09/22/2019, 5:09 AMktx
function by viewModels
how can i get parent Fragment's ViewModel
?
I have tried this private val homeViewModel by parentFragment!!.viewModels<HomeViewModel>()
but it did not workedtseisel
09/22/2019, 7:33 AMprivate 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.