rememberViewModelStoreOwner

Functions summary

ViewModelStoreOwner
@Composable
rememberViewModelStoreOwner(
    provider: ViewModelStoreProvider,
    key: Any?,
    savedStateRegistryOwner: SavedStateRegistryOwner?
)

Remembers a ViewModelStoreOwner scoped to the current composable using an existing provider.

Cmn
ViewModelStoreOwner
@Composable
rememberViewModelStoreOwner(
    parent: ViewModelStoreOwner?,
    savedStateRegistryOwner: SavedStateRegistryOwner?,
    defaultArgs: SavedState,
    defaultCreationExtras: CreationExtras,
    defaultFactory: ViewModelProvider.Factory
)

Remembers a ViewModelStoreOwner scoped to the current composable.

Cmn

Functions

rememberViewModelStoreOwner

@Composable
fun rememberViewModelStoreOwner(
    provider: ViewModelStoreProvider,
    key: Any? = currentCompositeKeyHashCode,
    savedStateRegistryOwner: SavedStateRegistryOwner? = LocalSavedStateRegistryOwner.current
): ViewModelStoreOwner

Remembers a ViewModelStoreOwner scoped to the current composable using an existing provider.

This function creates an owner unique to this specific call site in the composition hierarchy. While this composable is active, it maintains an active reference to the underlying ViewModelStore, preventing it from being cleared.

Note: Unlike many other scoped owners, ViewModels created with this owner are not automatically cleared simply because this composable leaves the composition. The ViewModelStore is only cleared when ViewModelStoreProvider.clearKey is explicitly called for this key.

This function is responsible for releasing its reference to the store when it leaves the composition, allowing the provider to perform cleanup if the store has been marked for clearing.

Parameters
provider: ViewModelStoreProvider

The ViewModelStoreProvider that manages the creation and cleanup of the underlying ViewModelStore.

key: Any? = currentCompositeKeyHashCode

A unique identifier for this call site to isolate its store from others. Defaults to currentCompositeKeyHashCode. If called multiple times in the same scope or loop, provide a custom key to ensure each instance gets its own ViewModelStore. A null key is valid and is treated as a distinct scope.

savedStateRegistryOwner: SavedStateRegistryOwner? = LocalSavedStateRegistryOwner.current

An optional SavedStateRegistryOwner to delegate saved state operations. When null, ViewModels created in this scope do not support saved state.

Returns
ViewModelStoreOwner

A ViewModelStoreOwner remembered across compositions and scoped to this call site.

rememberViewModelStoreOwner

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

Remembers a ViewModelStoreOwner scoped to the current composable.

This function creates an owner that is unique to this specific call site in the composition hierarchy. It allows creating ViewModels that are strictly scoped to this composable's lifecycle: they are created when this composable enters the composition and cleared immediately when it leaves.

The owner is linked to the parent, ensuring that configuration changes (like rotation) are handled correctly: the ViewModels survive rotation if the parent does, but are destroyed if the parent is destroyed.

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.

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

The ViewModelStoreOwner to use as the parent. Defaults to the owner from LocalViewModelStoreOwner.

savedStateRegistryOwner: SavedStateRegistryOwner? = LocalSavedStateRegistryOwner.current

An optional SavedStateRegistryOwner to delegate saved state operations. When null, ViewModels created in this scope do not support saved state.

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
ViewModelStoreOwner

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