📜  定义父片段视图模型 - 任何代码示例

📅  最后修改于: 2022-03-11 14:56:32.104000             🧑  作者: Mango

代码示例1
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() }
)