# rememberViewModelStoreProvider

Common/AllAndroid/JVM

## Functions summary

|---|---|---|
| `https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/ViewModelStoreProvider` | `@https://developer.android.com/reference/kotlin/androidx/compose/runtime/Composable https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/compose/rememberViewModelStoreProvider.composable#rememberViewModelStoreProvider(androidx.lifecycle.ViewModelStoreOwner,androidx.savedstate.SavedState,androidx.lifecycle.viewmodel.CreationExtras,androidx.lifecycle.ViewModelProvider.Factory)( parent: https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelStoreOwner?, defaultArgs: https://developer.android.com/reference/kotlin/androidx/savedstate/SavedState, defaultCreationExtras: https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/CreationExtras, defaultFactory: https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelProvider.Factory )` Remembers a new `https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/ViewModelStoreProvider` which creates a ViewModel scope linked to a parent found in the composition. | Cmn |
| `https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/ViewModelStoreProvider` | `@https://developer.android.com/reference/kotlin/androidx/compose/runtime/Composable https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/compose/rememberViewModelStoreProvider.composable#rememberViewModelStoreProvider(kotlin.Any,androidx.lifecycle.ViewModelStoreOwner,androidx.savedstate.SavedState,androidx.lifecycle.viewmodel.CreationExtras,androidx.lifecycle.ViewModelProvider.Factory)( key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?, parent: https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelStoreOwner?, defaultArgs: https://developer.android.com/reference/kotlin/androidx/savedstate/SavedState, defaultCreationExtras: https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/CreationExtras, defaultFactory: https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelProvider.Factory )` Remembers a new `https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/ViewModelStoreProvider` which creates a ViewModel scope linked to a parent found in the composition, using a specific `https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/compose/package-summary#rememberViewModelStoreProvider(kotlin.Any,androidx.lifecycle.ViewModelStoreOwner,androidx.savedstate.SavedState,androidx.lifecycle.viewmodel.CreationExtras,androidx.lifecycle.ViewModelProvider.Factory)`. | Cmn |

## Functions

### rememberViewModelStoreProvider

Cmn Artifact: [androidx.lifecycle:lifecycle-viewmodel-compose](https://developer.android.com/jetpack/androidx/releases/lifecycle) [View Source](https://cs.android.com/search?q=file:androidx/lifecycle/viewmodel/compose/RememberViewModelStoreProvider.kt+function:rememberViewModelStoreProvider)

```
@Composable
fun rememberViewModelStoreProvider(
    parent: ViewModelStoreOwner? = checkNotNull(LocalViewModelStoreOwner.current) {
            "CompositionLocal LocalViewModelStoreOwner not present"
        },
    defaultArgs: SavedState = savedState(),
    defaultCreationExtras: CreationExtras = parent.defaultViewModelCreationExtras,
    defaultFactory: ViewModelProvider.Factory = parent.defaultViewModelProviderFactory
): ViewModelStoreProvider
```

Remembers a new `https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/ViewModelStoreProvider` which creates a ViewModel scope linked to a parent found in the composition.

This function creates a provider scoped to this specific call site in the composition.

The provider's lifecycle is automatically managed. It is created only once and automatically disposed of when the composable leaves the composition. Crucially, it is aware of the parent's state and will survive configuration changes (like device rotation) if the parent does.

**Null parent:** If `https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/compose/package-summary#rememberViewModelStoreProvider(androidx.lifecycle.ViewModelStoreOwner,androidx.savedstate.SavedState,androidx.lifecycle.viewmodel.CreationExtras,androidx.lifecycle.ViewModelProvider.Factory)` is **EXPLICITLY** `null`, this creates a root provider that runs independently. By default, it requires a parent from the `https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/compose/LocalViewModelStoreOwner` and will throw an `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-state-exception/index.html` if one is not present.

```kotlin
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.remember
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewmodel.ViewModelStoreProvider
import androidx.lifecycle.viewmodel.compose.LocalViewModelStoreOwner
import androidx.lifecycle.viewmodel.compose.rememberViewModelStoreOwner
import androidx.lifecycle.viewmodel.compose.rememberViewModelStoreProvider
import androidx.lifecycle.viewmodel.compose.viewModel

// Creates a provider unique to this feature branch using hashCode.
val storeProvider = rememberViewModelStoreProvider()

// Use the provider to get an owner (without key)
val owner = rememberViewModelStoreOwner(provider = storeProvider)

CompositionLocalProvider(LocalViewModelStoreOwner provides owner) {
    val viewModel = viewModel { TestViewModel("isolated_data") }
}
```

| Parameters |
|---|---|
| `parent: https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelStoreOwner? = checkNotNull(LocalViewModelStoreOwner.current) { "CompositionLocal LocalViewModelStoreOwner not present" }` | The `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelStoreOwner` to use as the parent, or `null` if it is a root. Defaults to the owner from `https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/compose/LocalViewModelStoreOwner`. If this value changes, the provider will be recreated. |
| `defaultArgs: https://developer.android.com/reference/kotlin/androidx/savedstate/SavedState = savedState()` | The `https://developer.android.com/reference/kotlin/androidx/savedstate/SavedState` containing default arguments to be passed to ViewModels created in this scope. These arguments are merged with any default arguments in `https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/compose/package-summary#rememberViewModelStoreProvider(androidx.lifecycle.ViewModelStoreOwner,androidx.savedstate.SavedState,androidx.lifecycle.viewmodel.CreationExtras,androidx.lifecycle.ViewModelProvider.Factory)`. If the same key exists in both, the value from `https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/compose/package-summary#rememberViewModelStoreProvider(androidx.lifecycle.ViewModelStoreOwner,androidx.savedstate.SavedState,androidx.lifecycle.viewmodel.CreationExtras,androidx.lifecycle.ViewModelProvider.Factory)` takes precedence. This value is only read during the initial creation of the provider; subsequent recompositions with different values will not update the existing provider. |
| `defaultCreationExtras: https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/CreationExtras = parent.defaultViewModelCreationExtras` | The `https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/CreationExtras` to use. Defaults to the `https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/compose/package-summary#rememberViewModelStoreProvider(androidx.lifecycle.ViewModelStoreOwner,androidx.savedstate.SavedState,androidx.lifecycle.viewmodel.CreationExtras,androidx.lifecycle.ViewModelProvider.Factory)`'s default extras. This value is only read during the initial creation of the provider; subsequent recompositions with different values will not update or recreate the existing provider. |
| `defaultFactory: https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelProvider.Factory = parent.defaultViewModelProviderFactory` | The `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelProvider.Factory` to use for creating ViewModels in this scope. Defaults to the `https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/compose/package-summary#rememberViewModelStoreProvider(androidx.lifecycle.ViewModelStoreOwner,androidx.savedstate.SavedState,androidx.lifecycle.viewmodel.CreationExtras,androidx.lifecycle.ViewModelProvider.Factory)`'s default factory. This value is only read during the initial creation of the provider; subsequent recompositions with different values will not update or recreate the existing provider. |

| Returns |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/ViewModelStoreProvider` | A new `https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/ViewModelStoreProvider` that is remembered across compositions and scoped to this call site. |

### rememberViewModelStoreProvider

Cmn Artifact: [androidx.lifecycle:lifecycle-viewmodel-compose](https://developer.android.com/jetpack/androidx/releases/lifecycle) [View Source](https://cs.android.com/search?q=file:androidx/lifecycle/viewmodel/compose/RememberViewModelStoreProvider.kt+function:rememberViewModelStoreProvider)

```
@Composable
fun rememberViewModelStoreProvider(
    key: Any?,
    parent: ViewModelStoreOwner? = checkNotNull(LocalViewModelStoreOwner.current) {
            "CompositionLocal LocalViewModelStoreOwner not present"
        },
    defaultArgs: SavedState = savedState(),
    defaultCreationExtras: CreationExtras = parent.defaultViewModelCreationExtras,
    defaultFactory: ViewModelProvider.Factory = parent.defaultViewModelProviderFactory
): ViewModelStoreProvider
```

Remembers a new `https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/ViewModelStoreProvider` which creates a ViewModel scope linked to a parent found in the composition, using a specific `https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/compose/package-summary#rememberViewModelStoreProvider(kotlin.Any,androidx.lifecycle.ViewModelStoreOwner,androidx.savedstate.SavedState,androidx.lifecycle.viewmodel.CreationExtras,androidx.lifecycle.ViewModelProvider.Factory)`.

This function allows you to scope a `https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/ViewModelStoreProvider` to a custom `https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/compose/package-summary#rememberViewModelStoreProvider(kotlin.Any,androidx.lifecycle.ViewModelStoreOwner,androidx.savedstate.SavedState,androidx.lifecycle.viewmodel.CreationExtras,androidx.lifecycle.ViewModelProvider.Factory)`. This is useful in cases where using a key in different parts of the UI should yield the same state or instance (similar to how a ViewModel is shared). For example, you might use a key derived from navigation arguments, such as `ViewModelNavEntryDecorator.contentKey`, to share the same owner across different screens or components.

The provider's lifecycle is automatically managed. It is created only once and automatically disposed of when the composable leaves the composition. Crucially, it is aware of the parent's state and will survive configuration changes (like device rotation) if the parent does.

**Null parent:** If `https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/compose/package-summary#rememberViewModelStoreProvider(kotlin.Any,androidx.lifecycle.ViewModelStoreOwner,androidx.savedstate.SavedState,androidx.lifecycle.viewmodel.CreationExtras,androidx.lifecycle.ViewModelProvider.Factory)` is **EXPLICITLY** `null`, this creates a root provider that runs independently. By default, it requires a parent from the `https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/compose/LocalViewModelStoreOwner` and will throw an `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-state-exception/index.html` if one is not present.

```kotlin
import androidx.compose.runtime.remember
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewmodel.ViewModelStoreProvider
import androidx.lifecycle.viewmodel.compose.rememberViewModelStoreProvider

val featureKey = "shared_feature_flow"

// Creates a provider scoped to a custom key.
// This allows sharing the same provider (and its stores) across different parts of the UI
// that might not share the same parent composition node but share the same feature key.
val storeProvider = rememberViewModelStoreProvider(key = featureKey)

// Component A
FeaturePartA(storeProvider)

// Component B
FeaturePartB(storeProvider)
```

| Parameters |
|---|---|
| `key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?` | A unique identifier to isolate this provider from others. Providing the same `https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/compose/package-summary#rememberViewModelStoreProvider(kotlin.Any,androidx.lifecycle.ViewModelStoreOwner,androidx.savedstate.SavedState,androidx.lifecycle.viewmodel.CreationExtras,androidx.lifecycle.ViewModelProvider.Factory)` and `https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/compose/package-summary#rememberViewModelStoreProvider(kotlin.Any,androidx.lifecycle.ViewModelStoreOwner,androidx.savedstate.SavedState,androidx.lifecycle.viewmodel.CreationExtras,androidx.lifecycle.ViewModelProvider.Factory)` to multiple `https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/compose/rememberViewModelStoreProvider.composable#rememberViewModelStoreProvider(kotlin.Any,androidx.lifecycle.ViewModelStoreOwner,androidx.savedstate.SavedState,androidx.lifecycle.viewmodel.CreationExtras,androidx.lifecycle.ViewModelProvider.Factory)` calls will yield providers that share the same internal state, so matching child keys will share the same state. If this value changes, the provider will be recreated. |
| `parent: https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelStoreOwner? = checkNotNull(LocalViewModelStoreOwner.current) { "CompositionLocal LocalViewModelStoreOwner not present" }` | The `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelStoreOwner` to use as the parent, or `null` if it is a root. Defaults to the owner from `https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/compose/LocalViewModelStoreOwner`. If this value changes, the provider will be recreated. |
| `defaultArgs: https://developer.android.com/reference/kotlin/androidx/savedstate/SavedState = savedState()` | The `https://developer.android.com/reference/kotlin/androidx/savedstate/SavedState` containing default arguments to be passed to ViewModels created in this scope. These arguments are merged with any default arguments in `https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/compose/package-summary#rememberViewModelStoreProvider(kotlin.Any,androidx.lifecycle.ViewModelStoreOwner,androidx.savedstate.SavedState,androidx.lifecycle.viewmodel.CreationExtras,androidx.lifecycle.ViewModelProvider.Factory)`. If the same key exists in both, the value from `https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/compose/package-summary#rememberViewModelStoreProvider(kotlin.Any,androidx.lifecycle.ViewModelStoreOwner,androidx.savedstate.SavedState,androidx.lifecycle.viewmodel.CreationExtras,androidx.lifecycle.ViewModelProvider.Factory)` takes precedence. This value is only read during the initial creation of the provider; subsequent recompositions with different values will not update the existing provider. |
| `defaultCreationExtras: https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/CreationExtras = parent.defaultViewModelCreationExtras` | The `https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/CreationExtras` to use. Defaults to the `https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/compose/package-summary#rememberViewModelStoreProvider(kotlin.Any,androidx.lifecycle.ViewModelStoreOwner,androidx.savedstate.SavedState,androidx.lifecycle.viewmodel.CreationExtras,androidx.lifecycle.ViewModelProvider.Factory)`'s default extras. This value is only read during the initial creation of the provider; subsequent recompositions with different values will not update or recreate the existing provider. |
| `defaultFactory: https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelProvider.Factory = parent.defaultViewModelProviderFactory` | The `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelProvider.Factory` to use for creating ViewModels in this scope. Defaults to the `https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/compose/package-summary#rememberViewModelStoreProvider(kotlin.Any,androidx.lifecycle.ViewModelStoreOwner,androidx.savedstate.SavedState,androidx.lifecycle.viewmodel.CreationExtras,androidx.lifecycle.ViewModelProvider.Factory)`'s default factory. This value is only read during the initial creation of the provider; subsequent recompositions with different values will not update or recreate the existing provider. |

| Returns |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/ViewModelStoreProvider` | A new `https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/ViewModelStoreProvider` that is remembered across compositions and scoped to the provided `https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/compose/package-summary#rememberViewModelStoreProvider(kotlin.Any,androidx.lifecycle.ViewModelStoreOwner,androidx.savedstate.SavedState,androidx.lifecycle.viewmodel.CreationExtras,androidx.lifecycle.ViewModelProvider.Factory)`. |