Answers for "define parent fragment viewmodel"

0

define parent fragment viewmodel

Using Fragment-ktx libr in your app you can get viewModel as below in your app-> build.gradle

 implementation 'androidx.fragment:fragment-ktx:1.1.0'
// get ViewModel in ParentFragment as

 private val viewModel: DemoViewModel by viewModels()
// get same instance of ViewModel in ChildFragment as

 private val viewModel: DemoViewModel by viewModels(
    ownerProducer = { requireParentFragment() }
)
Posted by: Guest on December-03-2020

Browse Popular Code Answers by Language