Known direct subclasses
RunnableTransformScope

A TransformScope that manages a shared headless Composition and requires calling run to execute state transformations created via launchTransform.


A scope for launching state transformations via launchTransform.

Pass a TransformScope to components that only need to launch transformations without managing the underlying Composition lifecycle. To create a scope and run its transformations, use RunnableTransformScope and call RunnableTransformScope.run.

Summary

Public functions

DisposableState<T>
<T : Any?> launchTransform(
    initialState: T,
    container: SavedStateContainer,
    onUpdate: @Composable (T) -> T
)

Launches a Composable onUpdate transformation in this TransformScope and returns its result as a DisposableState.

Cmn

Public functions

launchTransform

fun <T : Any?> launchTransform(
    initialState: T,
    container: SavedStateContainer = SavedStateContainer(),
    onUpdate: @Composable (T) -> T
): DisposableState<T>

Launches a Composable onUpdate transformation in this TransformScope and returns its result as a DisposableState.

This function bridges reactive Compose state (or Flows collected as state) into a continuously updated State object outside a traditional Compose UI hierarchy. The transform block is automatically recomposed whenever any Compose state it reads is updated, and the newly returned value is pushed into the resulting State.

By default, this function uses an unattached SavedStateContainer. In order for androidx.compose.runtime.saveable.rememberSaveable inside onUpdate to work properly (saving and restoring state across configuration changes or process death), developers must provide their own SavedStateContainer, such as one obtained from SavedStateHandle.getOrCreateContainer:

class MyViewModel(
transformScope: TransformScope,
savedStateHandle: SavedStateHandle,
)
: ViewModel() {
val uiState: DisposableState<Int> = transformScope.launchTransform(
initialState = 0,
container = savedStateHandle.getOrCreateContainer("myTransform"),
) {
var count by rememberSaveable { mutableStateOf(0) }
count
}
}

The same is also possible with a androidx.savedstate.SavedStateRegistry via androidx.savedstate.SavedStateRegistry.getOrCreateContainer.

Calling DisposableState.dispose removes this transformation from the TransformScope so that it is no longer recomposed in the shared Composition.

Parameters
initialState: T

The initial value to populate the returned DisposableState before the first composition completes.

container: SavedStateContainer = SavedStateContainer()

The SavedStateContainer used to save and restore androidx.compose.runtime.saveable.rememberSaveable state. Developers must provide their own SavedStateContainer for rememberSaveable to persist and restore state properly.

onUpdate: @Composable (T) -> T

The Composable block that computes the updated value of type T.

Returns
DisposableState<T>

A DisposableState containing the latest result of the onUpdate block.