Reputation: 726
How to share same viewModel between dialog and dialogFragment? I know that viewModel can be shared in activity scope. But it is too big scope for me.
private val model: SharedViewModel by activityViewModels()
Unfortunately I don't have in a project navigation component.
Upvotes: 21
Views: 6711
Reputation: 5241
Use childFragmentManager
to show DialogFragment
Declare shared ViewModel
inside Fragment
by
private val sharedViewModel: YourViewModel by viewModels()
DialogFragment
declare ViewModel
byprivate val sharedViewModel: YourViewModel by viewModels(ownerProducer = { requireParentFragment() })
Upvotes: 35