# AbstractSavedStateViewModelFactory

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/AbstractSavedStateViewModelFactory.android.kt+class:androidx.lifecycle.AbstractSavedStateViewModelFactory) Added in [2.3.0](https://developer.android.com/jetpack/androidx/releases/lifecycle#2.3.0) Deprecated in [2.9.0](https://developer.android.com/jetpack/androidx/releases/lifecycle#2.9.0)

*** ** * ** ***

Common/AllAndroid/JVM
- android

  ```
  abstract class AbstractSavedStateViewModelFactory : ViewModelProvider.Factory
  ```

*** ** * ** ***

> [!CAUTION]
> **This class is deprecated.**   
> Use \`viewModelFactory\` or implement \`ViewModelProvider.Factory\`, combined with \`CreationExtras.createSavedStateHandle()\`.

Abstract `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelProvider.Factory` that creates a `https://developer.android.com/reference/kotlin/androidx/lifecycle/SavedStateHandle` for every requested `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModel`.

Subclasses implement the abstract `https://developer.android.com/reference/kotlin/androidx/lifecycle/AbstractSavedStateViewModelFactory#create(java.lang.Class,androidx.lifecycle.viewmodel.CreationExtras)` method to instantiate `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModel`s.

**Deprecated:** Use `https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/package-summary#viewModelFactory(kotlin.Function1)` or implement `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelProvider.Factory` directly, combined with `https://developer.android.com/reference/kotlin/androidx/lifecycle/package-summary#(androidx.lifecycle.viewmodel.CreationExtras).createSavedStateHandle()`. This base class creates a `https://developer.android.com/reference/kotlin/androidx/lifecycle/SavedStateHandle` for every `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModel`, even when unnecessary, causing overhead.

For example:

```kotlin
viewModelFactory { initializer { MyViewModel(createSavedStateHandle()) } }
```

## Summary

| ### Public constructors |
|---|---|
| `[AbstractSavedStateViewModelFactory](https://developer.android.com/reference/kotlin/androidx/lifecycle/AbstractSavedStateViewModelFactory#AbstractSavedStateViewModelFactory())()` **This function is deprecated.** Use \`viewModelFactory\` or implement \`ViewModelProvider.Factory\`, combined with \`CreationExtras.createSavedStateHandle()\`. | android |
| `[AbstractSavedStateViewModelFactory](https://developer.android.com/reference/kotlin/androidx/lifecycle/AbstractSavedStateViewModelFactory#AbstractSavedStateViewModelFactory(androidx.savedstate.SavedStateRegistryOwner,android.os.Bundle))( owner: https://developer.android.com/reference/kotlin/androidx/savedstate/SavedStateRegistryOwner, defaultArgs: https://developer.android.com/reference/android/os/Bundle.html? )` **This function is deprecated.** Use \`viewModelFactory\` or implement \`ViewModelProvider.Factory\`, combined with \`CreationExtras.createSavedStateHandle()\`. | android |

| ### Public functions |
|---|---|---|
| `open T` | `<T : https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModel> [create](https://developer.android.com/reference/kotlin/androidx/lifecycle/AbstractSavedStateViewModelFactory#create(java.lang.Class))(modelClass: https://developer.android.com/reference/java/lang/Class.html<T>)` **This function is deprecated.** Use \`viewModelFactory\` or implement \`ViewModelProvider.Factory\`, combined with \`CreationExtras.createSavedStateHandle()\`. | android |
| `open T` | `<T : https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModel> [create](https://developer.android.com/reference/kotlin/androidx/lifecycle/AbstractSavedStateViewModelFactory#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)` **This function is deprecated.** Use \`viewModelFactory\` or implement \`ViewModelProvider.Factory\`, combined with \`CreationExtras.createSavedStateHandle()\`. | android |

| ### Protected functions |
|---|---|---|
| `abstract T` | `<T : https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModel> [create](https://developer.android.com/reference/kotlin/androidx/lifecycle/AbstractSavedStateViewModelFactory#create(kotlin.String,java.lang.Class,androidx.lifecycle.SavedStateHandle))( key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html, modelClass: https://developer.android.com/reference/java/lang/Class.html<T>, handle: https://developer.android.com/reference/kotlin/androidx/lifecycle/SavedStateHandle )` **This function is deprecated.** Use \`viewModelFactory\` or implement \`ViewModelProvider.Factory\`, combined with \`CreationExtras.createSavedStateHandle()\`. | android |

### Inherited functions

From [androidx.lifecycle.ViewModelProvider.Factory](https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelProvider.Factory) `open T` `<T : https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModel> [create](https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelProvider.Factory#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)`

**This function is deprecated.** Use \`viewModelFactory\` or implement \`ViewModelProvider.Factory\`, combined with \`CreationExtras.createSavedStateHandle()\`.
android

## Public constructors

### AbstractSavedStateViewModelFactory

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

```
[AbstractSavedStateViewModelFactory](https://developer.android.com/reference/kotlin/androidx/lifecycle/AbstractSavedStateViewModelFactory#AbstractSavedStateViewModelFactory())()
```

> [!CAUTION]
> **This function is deprecated.**   
> Use \`viewModelFactory\` or implement \`ViewModelProvider.Factory\`, combined with \`CreationExtras.createSavedStateHandle()\`.

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

When constructed this way, the component for which the `https://developer.android.com/reference/kotlin/androidx/lifecycle/SavedStateHandle` is scoped must have called `https://developer.android.com/reference/kotlin/androidx/lifecycle/package-summary#(androidx.savedstate.SavedStateRegistryOwner).enableSavedStateHandles()`. See `https://developer.android.com/reference/kotlin/androidx/lifecycle/package-summary#(androidx.lifecycle.viewmodel.CreationExtras).createSavedStateHandle()` for more details.

### AbstractSavedStateViewModelFactory

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

```
[AbstractSavedStateViewModelFactory](https://developer.android.com/reference/kotlin/androidx/lifecycle/AbstractSavedStateViewModelFactory#AbstractSavedStateViewModelFactory(androidx.savedstate.SavedStateRegistryOwner,android.os.Bundle))(
    owner: SavedStateRegistryOwner,
    defaultArgs: Bundle?
)
```

> [!CAUTION]
> **This function is deprecated.**   
> Use \`viewModelFactory\` or implement \`ViewModelProvider.Factory\`, combined with \`CreationExtras.createSavedStateHandle()\`.

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

| Parameters |
|---|---|
| `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/AbstractSavedStateViewModelFactory#(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](https://developer.android.com/reference/kotlin/androidx/lifecycle/AbstractSavedStateViewModelFactory#create(java.lang.Class))(modelClass: Class<T>): T
```

> [!CAUTION]
> **This function is deprecated.**   
> Use \`viewModelFactory\` or implement \`ViewModelProvider.Factory\`, combined with \`CreationExtras.createSavedStateHandle()\`.

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

| Parameters |
|---|---|
| `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/AbstractSavedStateViewModelFactory#create(java.lang.Class)` |

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-argument-exception/index.html` | if the given `https://developer.android.com/reference/kotlin/androidx/lifecycle/AbstractSavedStateViewModelFactory#create(java.lang.Class)` is a local or anonymous class |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unsupported-operation-exception/index.html` | if this factory was constructed with the empty constructor, and therefore has no `https://developer.android.com/reference/kotlin/androidx/savedstate/SavedStateRegistryOwner` |

### create

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

```
open fun <T : ViewModel> [create](https://developer.android.com/reference/kotlin/androidx/lifecycle/AbstractSavedStateViewModelFactory#create(java.lang.Class,androidx.lifecycle.viewmodel.CreationExtras))(modelClass: Class<T>, extras: CreationExtras): T
```

> [!CAUTION]
> **This function is deprecated.**   
> Use \`viewModelFactory\` or implement \`ViewModelProvider.Factory\`, combined with \`CreationExtras.createSavedStateHandle()\`.

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

| Parameters |
|---|---|
| `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 |
| `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/AbstractSavedStateViewModelFactory#create(java.lang.Class,androidx.lifecycle.viewmodel.CreationExtras)` |

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-state-exception/index.html` | if the `https://developer.android.com/reference/kotlin/androidx/lifecycle/AbstractSavedStateViewModelFactory#create(java.lang.Class,androidx.lifecycle.viewmodel.CreationExtras)` do not contain `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelProvider.NewInstanceFactory.Companion#VIEW_MODEL_KEY()` |

## Protected functions

### create

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

```
protected abstract fun <T : ViewModel> [create](https://developer.android.com/reference/kotlin/androidx/lifecycle/AbstractSavedStateViewModelFactory#create(kotlin.String,java.lang.Class,androidx.lifecycle.SavedStateHandle))(
    key: String,
    modelClass: Class<T>,
    handle: SavedStateHandle
): T
```

> [!CAUTION]
> **This function is deprecated.**   
> Use \`viewModelFactory\` or implement \`ViewModelProvider.Factory\`, combined with \`CreationExtras.createSavedStateHandle()\`.

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

| Parameters |
|---|---|
| `key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html` | key associated with the requested `https://developer.android.com/reference/kotlin/androidx/lifecycle/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 |
| `handle: https://developer.android.com/reference/kotlin/androidx/lifecycle/SavedStateHandle` | `https://developer.android.com/reference/kotlin/androidx/lifecycle/SavedStateHandle` associated with 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/AbstractSavedStateViewModelFactory#create(kotlin.String,java.lang.Class,androidx.lifecycle.SavedStateHandle)` |