# SavedStateViewModelFactory

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

*** ** * ** ***

Common/AllAndroid/JVM
- Cmn

  ```
  class SavedStateViewModelFactory : ViewModelProvider.Factory
  ```

*** ** * ** ***

A `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelProvider.Factory` that creates `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModel`s with access to a `https://developer.android.com/reference/kotlin/androidx/lifecycle/SavedStateHandle` in their constructor.

## Summary

| ### Public constructors |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/lifecycle/SavedStateViewModelFactory#SavedStateViewModelFactory()()` Constructs this factory. | Cmn android |
| `https://developer.android.com/reference/kotlin/androidx/lifecycle/SavedStateViewModelFactory#SavedStateViewModelFactory(android.app.Application,androidx.savedstate.SavedStateRegistryOwner)( application: https://developer.android.com/reference/android/app/Application.html?, owner: https://developer.android.com/reference/kotlin/androidx/savedstate/SavedStateRegistryOwner )` Constructs a new `https://developer.android.com/reference/kotlin/androidx/lifecycle/SavedStateViewModelFactory`. | android |
| `https://developer.android.com/reference/kotlin/androidx/lifecycle/SavedStateViewModelFactory#SavedStateViewModelFactory(android.app.Application,androidx.savedstate.SavedStateRegistryOwner,android.os.Bundle)( application: https://developer.android.com/reference/android/app/Application.html?, owner: https://developer.android.com/reference/kotlin/androidx/savedstate/SavedStateRegistryOwner, defaultArgs: https://developer.android.com/reference/android/os/Bundle.html? )` Constructs a new `https://developer.android.com/reference/kotlin/androidx/lifecycle/SavedStateViewModelFactory`. | android |

| ### Public functions |
|---|---|---|
| `open T` | `<T : https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModel> https://developer.android.com/reference/kotlin/androidx/lifecycle/SavedStateViewModelFactory#create(java.lang.Class)(modelClass: https://developer.android.com/reference/java/lang/Class.html<T>)` {@inheritDoc} | android |
| `T` | `<T : https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModel> https://developer.android.com/reference/kotlin/androidx/lifecycle/SavedStateViewModelFactory#create(kotlin.String,java.lang.Class)(key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html, modelClass: https://developer.android.com/reference/java/lang/Class.html<T>)` Creates a new instance of the given `Class`. | android |
| `open T` | `<T : https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModel> https://developer.android.com/reference/kotlin/androidx/lifecycle/SavedStateViewModelFactory#create(java.lang.Class,androidx.lifecycle.viewmodel.CreationExtras)(modelClass: https://developer.android.com/reference/java/lang/Class.html<T>, extras: https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/CreationExtras)` {@inheritDoc} | android |
| `open T` | `<T : https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModel> https://developer.android.com/reference/kotlin/androidx/lifecycle/SavedStateViewModelFactory#create(kotlin.reflect.KClass,androidx.lifecycle.viewmodel.CreationExtras)(modelClass: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.reflect/-k-class/index.html<T>, extras: https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/CreationExtras)` Creates a new instance of the given `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelProvider.Factory#create(kotlin.reflect.KClass,androidx.lifecycle.viewmodel.CreationExtras)`. | Cmn android |

## Public constructors

### SavedStateViewModelFactory

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

```
SavedStateViewModelFactory()
```

Constructs this factory.

When using this factory, the component scoping the `https://developer.android.com/reference/kotlin/androidx/lifecycle/SavedStateHandle` must first invoke `https://developer.android.com/reference/kotlin/androidx/lifecycle/package-summary#(androidx.savedstate.SavedStateRegistryOwner).enableSavedStateHandles()`.

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/lifecycle/package-summary#(androidx.lifecycle.viewmodel.CreationExtras).createSavedStateHandle()` |   |

### SavedStateViewModelFactory

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

```
SavedStateViewModelFactory(
    application: Application?,
    owner: SavedStateRegistryOwner
)
```

Constructs a new `https://developer.android.com/reference/kotlin/androidx/lifecycle/SavedStateViewModelFactory`.

| Parameters |
|---|---|
| `application: https://developer.android.com/reference/android/app/Application.html?` | application instance. If null, `https://developer.android.com/reference/kotlin/androidx/lifecycle/AndroidViewModel` instances will not be supported. |
| `owner: https://developer.android.com/reference/kotlin/androidx/savedstate/SavedStateRegistryOwner` | `https://developer.android.com/reference/kotlin/androidx/savedstate/SavedStateRegistryOwner` that will provide restored state for created `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModel`s. Must implement `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelStoreOwner` to support state retention. |

### SavedStateViewModelFactory

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

```
SavedStateViewModelFactory(
    application: Application?,
    owner: SavedStateRegistryOwner,
    defaultArgs: Bundle?
)
```

Constructs a new `https://developer.android.com/reference/kotlin/androidx/lifecycle/SavedStateViewModelFactory`.

When constructed this way, any `https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/CreationExtras` provided to `https://developer.android.com/reference/kotlin/androidx/lifecycle/SavedStateViewModelFactory#create(kotlin.reflect.KClass,androidx.lifecycle.viewmodel.CreationExtras)` will override the state configured here. It is not possible to mix the arguments received here with `https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/CreationExtras`.

| Parameters |
|---|---|
| `application: https://developer.android.com/reference/android/app/Application.html?` | application instance. If null, `https://developer.android.com/reference/kotlin/androidx/lifecycle/AndroidViewModel` instances will not be supported. |
| `owner: https://developer.android.com/reference/kotlin/androidx/savedstate/SavedStateRegistryOwner` | `https://developer.android.com/reference/kotlin/androidx/savedstate/SavedStateRegistryOwner` that will provide restored state for created `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModel`s. Must implement `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelStoreOwner` to support state retention. |
| `defaultArgs: https://developer.android.com/reference/android/os/Bundle.html?` | default values to populate the `https://developer.android.com/reference/kotlin/androidx/lifecycle/SavedStateHandle` if no state is restored |

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-argument-exception/index.html` | if the `https://developer.android.com/reference/kotlin/androidx/lifecycle/SavedStateViewModelFactory#(android.app.Application,androidx.savedstate.SavedStateRegistryOwner,android.os.Bundle)` does not implement `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelStoreOwner` |

## Public functions

### create

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

```
open fun <T : ViewModel> create(modelClass: Class<T>): T
```

{@inheritDoc}

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-argument-exception/index.html` | if the given modelClass does not have a classname |

### create

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

```
fun <T : ViewModel> create(key: String, modelClass: Class<T>): T
```

Creates a new instance of the given `Class`.

| Parameters |
|---|---|
| `key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html` | a key associated with the requested ViewModel |
| `modelClass: https://developer.android.com/reference/java/lang/Class.html<T>` | `https://developer.android.com/reference/java/lang/Class.html` of the `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModel` to create |

| Returns |
|---|---|
| `T` | new `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModel` instance of type `https://developer.android.com/reference/kotlin/androidx/lifecycle/SavedStateViewModelFactory#create(kotlin.String,java.lang.Class)` |

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unsupported-operation-exception/index.html` | if there is no lifecycle |

### create

android

```
open fun <T : ViewModel> create(modelClass: Class<T>, extras: CreationExtras): T
```

{@inheritDoc}

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-state-exception/index.html` | if the provided extras do not provide a `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelProvider.NewInstanceFactory.Companion#VIEW_MODEL_KEY()` |

### create

Cmn android

```
open fun <T : ViewModel> create(modelClass: KClass<T>, extras: CreationExtras): T
```

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

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

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