activityViewModels
inline fun <VM : ViewModel> Fragment.activityViewModels(noinline extrasProducer: () -> CreationExtras? = null, noinline factoryProducer: () -> ViewModelProvider.Factory? = null): Lazy<VM>
Returns a property delegate to access parent activity's ViewModel, if factoryProducer is specified then ViewModelProvider.Factory returned by it will be used to create ViewModel first time. Otherwise, the activity's default factory will be used.
class MyFragment : Fragment() {
val viewmodel: MyViewModel by activityViewModels()
}Content copied to clipboard
This property can be accessed only after this Fragment is attached i.e., after Fragment.onAttach(), and access prior to that will result in IllegalArgumentException.