rememberViewModelStoreOwner

Functions summary

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

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

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

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

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,
    savedStateRegistryOwner: SavedStateRegistryOwner? = LocalSavedStateRegistryOwner.current
): ViewModelStoreOwner

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

This function creates an owner scoped to this specific call site in the composition.

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.

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
provider: ViewModelStoreProvider

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

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(
    key: Any?,
    provider: ViewModelStoreProvider,
    savedStateRegistryOwner: SavedStateRegistryOwner? = LocalSavedStateRegistryOwner.current
): ViewModelStoreOwner

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

This function allows you to scope a ViewModelStoreOwner 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.

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.

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

val storeProvider = rememberViewModelStoreProvider()
val pages = listOf("Page 1", "Page 2", "Page 3")

HorizontalPager(pageCount = pages.size) { page ->
    // Create a ViewModelStoreOwner for the specific page using the provider and a key.
    // This ensures each page gets its own isolated ViewModel instance.
    val pageOwner = rememberViewModelStoreOwner(provider = storeProvider, key = page)

    CompositionLocalProvider(LocalViewModelStoreOwner provides pageOwner) {
        val pageViewModel = viewModel { TestViewModel(pages[page]) }
        // Use pageViewModel
    }
}
Parameters
key: Any?

A unique identifier to isolate this store from others. Providing the same key and provider to multiple rememberViewModelStoreOwner calls will yield the same ViewModelStoreOwner and shared state.

provider: ViewModelStoreProvider

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

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 the provided key.

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.

import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewmodel.compose.LocalViewModelStoreOwner
import androidx.lifecycle.viewmodel.compose.rememberViewModelStoreOwner
import androidx.lifecycle.viewmodel.compose.viewModel

var showDetail by remember { mutableStateOf(false) }

if (showDetail) {
    // Creates a ViewModelStoreOwner scoped to this specific call site.
    // When showDetail becomes false, the owner leaves composition and ViewModels are cleared.
    val scopedOwner = rememberViewModelStoreOwner()

    CompositionLocalProvider(LocalViewModelStoreOwner provides scopedOwner) {
        val viewModel = viewModel { TestViewModel("detail_data") }
        // Use viewModel
    }
}
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.