rememberViewModelStoreProvider

Functions summary

ViewModelStoreProvider
@Composable
rememberViewModelStoreProvider(
    parent: ViewModelStoreOwner?,
    key: Any?,
    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

Functions

rememberViewModelStoreProvider

@Composable
fun rememberViewModelStoreProvider(
    parent: ViewModelStoreOwner? = checkNotNull(LocalViewModelStoreOwner.current) { "CompositionLocal LocalViewModelStoreOwner not present" },
    key: Any? = currentCompositeKeyHashCode,
    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 composable creates a provider that links to any parent found in the composition, forming a parent-child relationship. If no parent exists, it automatically becomes a new root provider. This is useful for isolating ViewModel instances within specific UI sections, such as a self-contained feature screen, dialog, or tab, ensuring they are cleared when that section is removed.

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.

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.

key: Any? = currentCompositeKeyHashCode

A unique identifier for this call site to isolate its provider 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 ViewModelStoreProvider. A null key is valid and is treated as a distinct scope.

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.