# FragmentViewModelLazyKt

Added in 1.9.1

*** ** * ** ***


```
public final class FragmentViewModelLazyKt
```

<br />

*** ** * ** ***

## Summary

| ### Public methods |
|---|---|
| `static final @https://developer.android.com/reference/androidx/annotation/NonNull https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-lazy/index.html<@https://developer.android.com/reference/androidx/annotation/NonNull VM>` | `@https://developer.android.com/reference/androidx/annotation/MainThread <VM extends https://developer.android.com/reference/androidx/lifecycle/ViewModel> https://developer.android.com/reference/androidx/fragment/app/FragmentViewModelLazyKt#(androidx.fragment.app.Fragment).activityViewModels(kotlin.Function0,kotlin.Function0)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/fragment/app/Fragment receiver, Function0<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/viewmodel/CreationExtras> extrasProducer, Function0<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider.Factory> factoryProducer )` Returns a property delegate to access parent activity's `https://developer.android.com/reference/androidx/lifecycle/ViewModel`, if `https://developer.android.com/reference/androidx/fragment/app/package-summary#(androidx.fragment.app.Fragment).activityViewModels(kotlin.Function0,kotlin.Function0)` is specified then `https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider.Factory` returned by it will be used to create `https://developer.android.com/reference/androidx/lifecycle/ViewModel` first time. |
| `static final @https://developer.android.com/reference/androidx/annotation/NonNull https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-lazy/index.html<@https://developer.android.com/reference/androidx/annotation/NonNull VM>` | `@https://developer.android.com/reference/androidx/annotation/MainThread <VM extends https://developer.android.com/reference/androidx/lifecycle/ViewModel> https://developer.android.com/reference/androidx/fragment/app/FragmentViewModelLazyKt#(androidx.fragment.app.Fragment).createViewModelLazy(kotlin.reflect.KClass,kotlin.Function0,kotlin.Function0,kotlin.Function0)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/fragment/app/Fragment receiver, @https://developer.android.com/reference/androidx/annotation/NonNull https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.reflect/-k-class/index.html<@https://developer.android.com/reference/androidx/annotation/NonNull VM> viewModelClass, @https://developer.android.com/reference/androidx/annotation/NonNull Function0<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/ViewModelStore> storeProducer, @https://developer.android.com/reference/androidx/annotation/NonNull Function0<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/viewmodel/CreationExtras> extrasProducer, Function0<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider.Factory> factoryProducer )` Helper method for creation of `https://developer.android.com/reference/androidx/lifecycle/ViewModelLazy`, that resolves `null` passed as `https://developer.android.com/reference/androidx/fragment/app/package-summary#(androidx.fragment.app.Fragment).createViewModelLazy(kotlin.reflect.KClass,kotlin.Function0,kotlin.Function0,kotlin.Function0)` to the default factory. |
| `static final @https://developer.android.com/reference/androidx/annotation/NonNull https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-lazy/index.html<@https://developer.android.com/reference/androidx/annotation/NonNull VM>` | `@https://developer.android.com/reference/androidx/annotation/MainThread <VM extends https://developer.android.com/reference/androidx/lifecycle/ViewModel> https://developer.android.com/reference/androidx/fragment/app/FragmentViewModelLazyKt#(androidx.fragment.app.Fragment).viewModels(kotlin.Function0,kotlin.Function0,kotlin.Function0)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/fragment/app/Fragment receiver, @https://developer.android.com/reference/androidx/annotation/NonNull Function0<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/ViewModelStoreOwner> ownerProducer, Function0<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/viewmodel/CreationExtras> extrasProducer, Function0<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider.Factory> factoryProducer )` Returns a property delegate to access `https://developer.android.com/reference/androidx/lifecycle/ViewModel` by **default** scoped to this `https://developer.android.com/reference/androidx/fragment/app/Fragment`: |

## Public methods

### activityViewModels

Artifact: [androidx.fragment:fragment](https://developer.android.com/jetpack/androidx/releases/fragment) [View Source](https://cs.android.com/search?q=file:androidx/fragment/app/FragmentViewModelLazy.kt+function:activityViewModels)

```
@MainThread
public static final @NonNull Lazy<@NonNull VM> <VM extends ViewModel> activityViewModels(
    @NonNull Fragment receiver,
    Function0<@NonNull CreationExtras> extrasProducer,
    Function0<@NonNull ViewModelProvider.Factory> factoryProducer
)
```

Returns a property delegate to access parent activity's `https://developer.android.com/reference/androidx/lifecycle/ViewModel`, if `https://developer.android.com/reference/androidx/fragment/app/package-summary#(androidx.fragment.app.Fragment).activityViewModels(kotlin.Function0,kotlin.Function0)` is specified then `https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider.Factory` returned by it will be used to create `https://developer.android.com/reference/androidx/lifecycle/ViewModel` first time. Otherwise, the activity's default factory will be used.

```
class MyFragment : Fragment() {
    val viewmodel: MyViewModel by activityViewModels()
}
```

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.

### createViewModelLazy

Artifact: [androidx.fragment:fragment](https://developer.android.com/jetpack/androidx/releases/fragment) [View Source](https://cs.android.com/search?q=file:androidx/fragment/app/FragmentViewModelLazy.kt+function:createViewModelLazy)

```
@MainThread
public static final @NonNull Lazy<@NonNull VM> <VM extends ViewModel> createViewModelLazy(
    @NonNull Fragment receiver,
    @NonNull KClass<@NonNull VM> viewModelClass,
    @NonNull Function0<@NonNull ViewModelStore> storeProducer,
    @NonNull Function0<@NonNull CreationExtras> extrasProducer,
    Function0<@NonNull ViewModelProvider.Factory> factoryProducer
)
```

Helper method for creation of `https://developer.android.com/reference/androidx/lifecycle/ViewModelLazy`, that resolves `null` passed as `https://developer.android.com/reference/androidx/fragment/app/package-summary#(androidx.fragment.app.Fragment).createViewModelLazy(kotlin.reflect.KClass,kotlin.Function0,kotlin.Function0,kotlin.Function0)` to the default factory.

This method also takes a `https://developer.android.com/reference/androidx/lifecycle/viewmodel/CreationExtras` producer that provides default extras to the created `https://developer.android.com/reference/androidx/lifecycle/ViewModel`.

### viewModels

Artifact: [androidx.fragment:fragment](https://developer.android.com/jetpack/androidx/releases/fragment) [View Source](https://cs.android.com/search?q=file:androidx/fragment/app/FragmentViewModelLazy.kt+function:viewModels)

```
@MainThread
public static final @NonNull Lazy<@NonNull VM> <VM extends ViewModel> viewModels(
    @NonNull Fragment receiver,
    @NonNull Function0<@NonNull ViewModelStoreOwner> ownerProducer,
    Function0<@NonNull CreationExtras> extrasProducer,
    Function0<@NonNull ViewModelProvider.Factory> factoryProducer
)
```

Returns a property delegate to access `https://developer.android.com/reference/androidx/lifecycle/ViewModel` by **default** scoped to this `https://developer.android.com/reference/androidx/fragment/app/Fragment`:

```
class MyFragment : Fragment() {
    val viewmodel: MyViewModel by viewModels()
}
```

Custom `https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider.Factory` can be defined via `https://developer.android.com/reference/androidx/fragment/app/package-summary#(androidx.fragment.app.Fragment).viewModels(kotlin.Function0,kotlin.Function0,kotlin.Function0)` parameter, factory returned by it will be used to create `https://developer.android.com/reference/androidx/lifecycle/ViewModel`:

```
class MyFragment : Fragment() {
    val viewmodel: MyViewModel by viewModels { myFactory }
}
```

Default scope may be overridden with parameter `https://developer.android.com/reference/androidx/fragment/app/package-summary#(androidx.fragment.app.Fragment).viewModels(kotlin.Function0,kotlin.Function0,kotlin.Function0)`:

```
class MyFragment : Fragment() {
    val viewmodel: MyViewModel by viewModels ({requireParentFragment()})
}
```

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.