KScreen

fun <State, VM : KViewModel<State>> KScreen(viewModel: VM, navController: NavController, onBack: (state: State, viewModel: VM) -> Unit? = null, onEvent: (state: State, viewModel: VM, event: Any) -> Unit = { _, _, _ -> }, content: @Composable (state: State, viewModel: VM) -> Unit)(source)

Legacy lifecycle-aware screen wrapper for KViewModel-driven Composables.

KScreen is the original screen wrapper in the Kindling hierarchy. It centralizes the boilerplate required for a standard feature screen:

  • State collection: Automatically collects the ViewModel's KViewModel.state and provides it to the content block.

  • Event dispatching: One-shot side effects emitted via KViewModel.events are handled here. If an event implements KDestination, it triggers automatic navigation.

  • Back press handling: Optional custom handling for the system back button, with automatic focus clearing.

Basic usage

@Composable
fun ProfileScreen(
viewModel: ProfileViewModel,
navController: NavController
) {
KScreen(
viewModel = viewModel,
navController = navController,
onEvent = { state, vm, event ->
when (event) {
is ProfileEvent.ShowToast -> {
// Handle toast
}
}
}
) { state, vm ->
// Screen content
ProfileContent(
name = state.name,
onLogout = vm::onLogoutClick
)
}
}

Automatic Navigation

Any event emitted by the ViewModel that implements KDestination is automatically intercepted and used to navigate via the provided navController.

Parameters

viewModel

The KViewModel instance driving this screen.

navController

NavController used for automatic navigation when KDestination events occur.

onBack

Optional back press handler. Receives the latest state and ViewModel. Focus is cleared automatically before this callback is invoked.

onEvent

Handler for non-navigation events emitted by viewModel. Receives the latest state, the ViewModel, and the event object.

content

The main UI content of the screen.

Type Parameters

State

UI state type produced by viewModel.

VM

ViewModel type, must extend KViewModel<State>.

See also