# ViewModelProvider.NewInstanceFactory

Artifact: [androidx.lifecycle:lifecycle-viewmodel](https://developer.android.com/jetpack/androidx/releases/lifecycle) [View Source](https://cs.android.com/search?q=file:androidx/lifecycle/ViewModelProvider.android.kt+class:androidx.lifecycle.ViewModelProvider.NewInstanceFactory) Added in [2.0.0](https://developer.android.com/jetpack/androidx/releases/lifecycle#2.0.0)

*** ** * ** ***


```
public class ViewModelProvider.NewInstanceFactory implements ViewModelProvider.Factory
```

<br />

Known direct subclasses [ViewModelProvider.AndroidViewModelFactory](https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider.AndroidViewModelFactory)

|---|---|
| `https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider.AndroidViewModelFactory` | Factory that creates `https://developer.android.com/reference/androidx/lifecycle/AndroidViewModel` and `https://developer.android.com/reference/androidx/lifecycle/ViewModel` instances. |

*** ** * ** ***

Factory that creates `https://developer.android.com/reference/androidx/lifecycle/ViewModel` instances by calling their no-argument constructor.

## Summary

| ### Public fields |
|---|---|
| `static final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/viewmodel/CreationExtras.Key<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html>` | `https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider.NewInstanceFactory.Companion#VIEW_MODEL_KEY()` A `https://developer.android.com/reference/androidx/lifecycle/viewmodel/CreationExtras.Key` used to retrieve the key associated with a requested `https://developer.android.com/reference/androidx/lifecycle/ViewModel`. |

| ### Public constructors |
|---|
| `https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider.NewInstanceFactory#NewInstanceFactory()()` |

| ### Public methods |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull T` | `<T extends https://developer.android.com/reference/androidx/lifecycle/ViewModel> https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider.NewInstanceFactory#create(java.lang.Class)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/Class.html<@https://developer.android.com/reference/androidx/annotation/NonNull T> modelClass)` Creates a new instance of the given `https://developer.android.com/reference/java/lang/Class.html`. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull T` | `<T extends https://developer.android.com/reference/androidx/lifecycle/ViewModel> https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider.NewInstanceFactory#create(java.lang.Class,androidx.lifecycle.viewmodel.CreationExtras)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/Class.html<@https://developer.android.com/reference/androidx/annotation/NonNull T> modelClass, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/viewmodel/CreationExtras extras )` Creates a new instance of the given `https://developer.android.com/reference/java/lang/Class.html`. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull T` | `<T extends https://developer.android.com/reference/androidx/lifecycle/ViewModel> https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider.NewInstanceFactory#create(kotlin.reflect.KClass,androidx.lifecycle.viewmodel.CreationExtras)( @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 T> modelClass, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/viewmodel/CreationExtras extras )` Creates a new instance of the given `https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider.Factory#create(kotlin.reflect.KClass,androidx.lifecycle.viewmodel.CreationExtras)`. |

## Public fields

### VIEW_MODEL_KEY

```
public static final @NonNull CreationExtras.Key<@NonNull String> VIEW_MODEL_KEY
```

A `https://developer.android.com/reference/androidx/lifecycle/viewmodel/CreationExtras.Key` used to retrieve the key associated with a requested `https://developer.android.com/reference/androidx/lifecycle/ViewModel`.

The `https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider` automatically includes the key in the `https://developer.android.com/reference/androidx/lifecycle/viewmodel/CreationExtras` passed to `https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider.Factory`. This applies to keys generated by either of these usage patterns:

- `ViewModelProvider.get(key, MyViewModel::class)`: provided `key` is used.

- `ViewModelProvider.get(MyViewModel::class)`: generates a `key` from given `class`.

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider.Companion#VIEW_MODEL_KEY()` |   |

## Public constructors

### NewInstanceFactory

Added in [2.0.0](https://developer.android.com/jetpack/androidx/releases/lifecycle#2.0.0)

```
public NewInstanceFactory()
```

## Public methods

### create

```
public @NonNull T <T extends ViewModel> create(@NonNull Class<@NonNull T> modelClass)
```

Creates a new instance of the given `https://developer.android.com/reference/java/lang/Class.html`.

Default implementation throws `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unsupported-operation-exception/index.html`.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/Class.html<@https://developer.android.com/reference/androidx/annotation/NonNull T> modelClass` | `https://developer.android.com/reference/java/lang/Class.html` of the `https://developer.android.com/reference/androidx/lifecycle/ViewModel` to create |

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull T` | new `https://developer.android.com/reference/androidx/lifecycle/ViewModel` instance of type `https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider.Factory#create(java.lang.Class)` |

### create

```
public @NonNull T <T extends ViewModel> create(
    @NonNull Class<@NonNull T> modelClass,
    @NonNull CreationExtras extras
)
```

Creates a new instance of the given `https://developer.android.com/reference/java/lang/Class.html`.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/Class.html<@https://developer.android.com/reference/androidx/annotation/NonNull T> modelClass` | `https://developer.android.com/reference/java/lang/Class.html` of the `https://developer.android.com/reference/androidx/lifecycle/ViewModel` to create |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/viewmodel/CreationExtras extras` | `https://developer.android.com/reference/androidx/lifecycle/viewmodel/CreationExtras` passed to the `https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider.Factory` to create the `https://developer.android.com/reference/androidx/lifecycle/ViewModel` |

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull T` | new `https://developer.android.com/reference/androidx/lifecycle/ViewModel` instance of type `https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider.Factory#create(java.lang.Class,androidx.lifecycle.viewmodel.CreationExtras)` |

### create

```
public @NonNull T <T extends ViewModel> create(
    @NonNull KClass<@NonNull T> modelClass,
    @NonNull CreationExtras extras
)
```

Creates a new instance of the given `https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider.Factory#create(kotlin.reflect.KClass,androidx.lifecycle.viewmodel.CreationExtras)`.

| Parameters |
|---|---|
| `@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 T> modelClass` | `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.reflect/-k-class/index.html` of the `https://developer.android.com/reference/androidx/lifecycle/ViewModel` to create |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/viewmodel/CreationExtras extras` | `https://developer.android.com/reference/androidx/lifecycle/viewmodel/CreationExtras` passed to the `https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider.Factory` to create the `https://developer.android.com/reference/androidx/lifecycle/ViewModel` |

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull T` | new `https://developer.android.com/reference/androidx/lifecycle/ViewModel` instance of type `https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider.Factory#create(kotlin.reflect.KClass,androidx.lifecycle.viewmodel.CreationExtras)` |