在子Fragment中获取父Fragment的 ViewModel

https://stackoverflow.com/questions/59952673/how-to-get-an-instance-of-viewmodel-in-activity-in-2020

// get ViewModel in Activity or Fragment as
private val viewModel: MainActivityViewModel by viewModels()

// If you want to get same instance of ViewModel in ChildFragment as
 private val viewModel: MainActivityViewModel by viewModels(
    ownerProducer = { requireParentFragment() }
)

你可能感兴趣的:(在子Fragment中获取父Fragment的 ViewModel)