# ActivityViewModelLazyKt

Added in 1.9.0

*** ** * ** ***


```
public final class ActivityViewModelLazyKt
```

<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/activity/ActivityViewModelLazyKt#(androidx.activity.ComponentActivity).viewModels(kotlin.Function0,kotlin.Function0)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/activity/ComponentActivity 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 `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-lazy/index.html` delegate to access the ComponentActivity's ViewModel, if `https://developer.android.com/reference/androidx/activity/package-summary#(androidx.activity.ComponentActivity).viewModels(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 T>` | `@https://developer.android.com/reference/androidx/annotation/MainThread <T extends https://developer.android.com/reference/androidx/lifecycle/ViewModel> https://developer.android.com/reference/androidx/activity/ActivityViewModelLazyKt#(androidx.activity.ComponentActivity).viewModels(kotlin.String,kotlin.Function0,kotlin.Function0)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/activity/ComponentActivity receiver, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html key, 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 `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-lazy/index.html` delegate for this `https://developer.android.com/reference/androidx/activity/ComponentActivity`'s `https://developer.android.com/reference/androidx/lifecycle/ViewModel` identified by `https://developer.android.com/reference/androidx/activity/package-summary#(androidx.activity.ComponentActivity).viewModels(kotlin.String,kotlin.Function0,kotlin.Function0)`. |

## Public methods

### viewModels

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

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

Returns a `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-lazy/index.html` delegate to access the ComponentActivity's ViewModel, if `https://developer.android.com/reference/androidx/activity/package-summary#(androidx.activity.ComponentActivity).viewModels(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.

```
class MyComponentActivity : ComponentActivity() {
    val viewmodel: MyViewModel by viewModels()
}
```

This property can be accessed only after the Activity is attached to the Application, and access prior to that will result in IllegalArgumentException.

### viewModels

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

```
@MainThread
public static final @NonNull Lazy<@NonNull T> <T extends ViewModel> viewModels(
    @NonNull ComponentActivity receiver,
    @NonNull String key,
    Function0<@NonNull CreationExtras> extrasProducer,
    Function0<@NonNull ViewModelProvider.Factory> factoryProducer
)
```

Returns a `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-lazy/index.html` delegate for this `https://developer.android.com/reference/androidx/activity/ComponentActivity`'s `https://developer.android.com/reference/androidx/lifecycle/ViewModel` identified by `https://developer.android.com/reference/androidx/activity/package-summary#(androidx.activity.ComponentActivity).viewModels(kotlin.String,kotlin.Function0,kotlin.Function0)`.

Delegates with the same `https://developer.android.com/reference/androidx/activity/package-summary#(androidx.activity.ComponentActivity).viewModels(kotlin.String,kotlin.Function0,kotlin.Function0)` and `https://developer.android.com/reference/androidx/lifecycle/ViewModel` type share the same instance. Different keys allow multiple instances of the same `https://developer.android.com/reference/androidx/lifecycle/ViewModel` type in this activity.

```
class MyComponentActivity : ComponentActivity() {
    val primaryViewModel: MyViewModel by viewModels(key = "primary")
    val secondaryViewModel: MyViewModel by viewModels(key = "secondary")
}
```

This property can be accessed only after the Activity is attached to the Application. Access before that results in an `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-argument-exception/index.html`.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html key` | identifier used to store and retrieve the `https://developer.android.com/reference/androidx/lifecycle/ViewModel` |
| `Function0<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/viewmodel/CreationExtras> extrasProducer` | producer of the `https://developer.android.com/reference/androidx/lifecycle/viewmodel/CreationExtras` used to create the `https://developer.android.com/reference/androidx/lifecycle/ViewModel` |
| `Function0<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider.Factory> factoryProducer` | producer of the `https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider.Factory` used to create the `https://developer.android.com/reference/androidx/lifecycle/ViewModel` |