# ViewModelProvider.Factory

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.+class:androidx.lifecycle.ViewModelProvider.Factory) Added in [2.0.0](https://developer.android.com/jetpack/androidx/releases/lifecycle#2.0.0)

*** ** * ** ***


```
public interface ViewModelProvider.Factory
```

<br />

Known direct subclasses [AbstractSavedStateViewModelFactory](https://developer.android.com/reference/androidx/lifecycle/AbstractSavedStateViewModelFactory), [SavedStateViewModelFactory](https://developer.android.com/reference/androidx/lifecycle/SavedStateViewModelFactory), [ViewModelProvider.NewInstanceFactory](https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider.NewInstanceFactory)

|---|---|
| `https://developer.android.com/reference/androidx/lifecycle/AbstractSavedStateViewModelFactory` | **This class is deprecated.** Use \`viewModelFactory\` or implement \`ViewModelProvider.Factory\`, combined with \`CreationExtras.createSavedStateHandle()\`. |
| `https://developer.android.com/reference/androidx/lifecycle/SavedStateViewModelFactory` | A `https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider.Factory` that creates `https://developer.android.com/reference/androidx/lifecycle/ViewModel`s with access to a `https://developer.android.com/reference/androidx/lifecycle/SavedStateHandle` in their constructor. |
| `https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider.NewInstanceFactory` | Simple factory, which calls empty constructor on the give class. |

Known indirect 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. |

*** ** * ** ***

Implementations of the `https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider.Factory` interface are responsible for instantiating `https://developer.android.com/reference/androidx/lifecycle/ViewModel`s.

## Summary

| ### Public methods |
|---|---|
| `default @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.Factory#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`. |
| `default @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.Factory#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`. |
| `default static final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider.Factory` | `https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider.Factory#from(kotlin.Array)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/viewmodel/ViewModelInitializer<@https://developer.android.com/reference/androidx/annotation/NonNull ?>... initializers)` Creates an `https://developer.android.com/reference/androidx/lifecycle/viewmodel/InitializerViewModelFactory` using the given `https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider.Factory.Companion#from(kotlin.Array)`. |

## Public methods

### create

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

```
default @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

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

```
default @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)` |

### from

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

```
default static final @NonNull ViewModelProvider.Factory from(@NonNull ViewModelInitializer<@NonNull ?>... initializers)
```

Creates an `https://developer.android.com/reference/androidx/lifecycle/viewmodel/InitializerViewModelFactory` using the given `https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider.Factory.Companion#from(kotlin.Array)`.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/viewmodel/ViewModelInitializer<@https://developer.android.com/reference/androidx/annotation/NonNull ?>... initializers` | class initializer pairs used to create simple ViewModels |

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/lifecycle/viewmodel/InitializerViewModelFactory` |   |