rememberViewModelStoreProvider

Functions summary

ViewModelStoreProvider
@Composable
rememberViewModelStoreProvider(
    parent: ViewModelStoreOwner?,
    defaultArgs: SavedState,
    defaultCreationExtras: CreationExtras,
    defaultFactory: ViewModelProvider.Factory
)

Remembers a new ViewModelStoreProvider which creates a ViewModel scope linked to a parent found in the composition.

Cmn
ViewModelStoreProvider
@Composable
rememberViewModelStoreProvider(
    key: Any?,
    parent: ViewModelStoreOwner?,
    defaultArgs: SavedState,
    defaultCreationExtras: CreationExtras,
    defaultFactory: ViewModelProvider.Factory
)

Remembers a new ViewModelStoreProvider which creates a ViewModel scope linked to a parent found in the composition, using a specific key.

Cmn

Functions

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 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 parent is EXPLICITLY null, this creates a root provider that runs independently. By default, it requires a parent from the LocalViewModelStoreOwner and will throw an IllegalStateException if one is not present.

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: ViewModelStoreOwner? = checkNotNull(LocalViewModelStoreOwner.current) { "CompositionLocal LocalViewModelStoreOwner not present" }

The ViewModelStoreOwner to use as the parent, or null if it is a root. Defaults to the owner from LocalViewModelStoreOwner.

defaultArgs: SavedState = savedState()

The SavedState containing default arguments to be passed to ViewModels created in this scope. These arguments are merged with any default arguments in defaultCreationExtras. If the same key exists in both, the value from defaultArgs takes precedence.

defaultCreationExtras: CreationExtras = parent.defaultViewModelCreationExtras

The CreationExtras to use. Defaults to the parent's default extras.

defaultFactory: ViewModelProvider.Factory = parent.defaultViewModelProviderFactory

The ViewModelProvider.Factory to use for creating ViewModels in this scope. Defaults to the parent's default factory.

Returns
ViewModelStoreProvider

A new ViewModelStoreProvider that is remembered across compositions and scoped to this call site.

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 ViewModelStoreProvider which creates a ViewModel scope linked to a parent found in the composition, using a specific key.

This function allows you to scope a ViewModelStoreProvider to a custom key. 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 parent is EXPLICITLY null, this creates a root provider that runs independently. By default, it requires a parent from the LocalViewModelStoreOwner and will throw an IllegalStateException if one is not present.

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: Any?

A unique identifier to isolate this provider from others. Providing the same key and parent to multiple rememberViewModelStoreProvider calls will yield providers that share the same internal state, so matching child keys will share the same state.

parent: ViewModelStoreOwner? = checkNotNull(LocalViewModelStoreOwner.current) { "CompositionLocal LocalViewModelStoreOwner not present" }

The ViewModelStoreOwner to use as the parent, or null if it is a root. Defaults to the owner from LocalViewModelStoreOwner.

defaultArgs: SavedState = savedState()

The SavedState containing default arguments to be passed to ViewModels created in this scope. These arguments are merged with any default arguments in defaultCreationExtras. If the same key exists in both, the value from defaultArgs takes precedence.

defaultCreationExtras: CreationExtras = parent.defaultViewModelCreationExtras

The CreationExtras to use. Defaults to the parent's default extras.

defaultFactory: ViewModelProvider.Factory = parent.defaultViewModelProviderFactory

The ViewModelProvider.Factory to use for creating ViewModels in this scope. Defaults to the parent's default factory.

Returns
ViewModelStoreProvider

A new ViewModelStoreProvider that is remembered across compositions and scoped to the provided key.