Results
A destination whose key implements NavigationKey.WithResult<R> returns a value of type R to the caller. The caller creates a NavigationResultChannel<R> with registerForNavigationResult and opens the destination through the channel. The channel calls a callback when the destination completes or closes.
Returning and receiving a result
A key declares a result type by implementing NavigationKey.WithResult<R>:
@Serializable
data class SelectDate(
val minDate: LocalDate? = null,
val maxDate: LocalDate? = null,
) : NavigationKey.WithResult<LocalDate>
The destination returns the result with navigation.complete(value):
@Composable
@NavigationDestination(SelectDate::class)
fun SelectDateScreen() {
val navigation = navigationHandle<SelectDate>()
// ...
Button(onClick = { navigation.complete(today) }) { Text("Use today") }
}
A caller receives the result through a NavigationResultChannel<R>:
val getDate = registerForNavigationResult<LocalDate>(
onCompleted = { date -> /* use the date */ },
)
Button(onClick = { getDate.open(SelectDate()) }) { Text("Pick a date") }
In a Composable
registerForNavigationResult<R> is a Composable function that returns a NavigationResultChannel<R>. Call open(key) on the channel:
@Composable
@NavigationDestination(Home::class)
fun HomeScreen() {
val navigation = navigationHandle<Home>()
val pickDate = registerForNavigationResult<LocalDate>(
onClosed = { /* SelectDate closed without a result */ },
onCompleted = { date ->
// onCompleted is a callback, not a suspending function.
// Compose state can be updated here.
},
)
Button(onClick = { pickDate.open(SelectDate(maxDate = today)) }) {
Text("Pick a date")
}
}
The channel’s id is the destination’s instance id plus the composite key hash of the registerForNavigationResult call. A channel created by the same call after recomposition or process death has the same id and receives the result.
onCompleted and onClosed
onCompleted(value)is called when the opened destination callsnavigation.complete(value).onClosed()is called when the opened destination closes without completing, for example throughnavigation.close()or a back press.
onCompleted is required. onClosed defaults to a no-op.
Without a result type
For a key that does not implement WithResult<R>, registerForNavigationResult has an overload with no type argument. The onCompleted callback of the overload takes no value:
@Serializable
data class ConfirmDelete(val itemName: String) : NavigationKey
val confirmDelete = registerForNavigationResult(
onCompleted = { /* user confirmed */ },
onClosed = { /* user cancelled */ },
)
Button(onClick = { confirmDelete.open(ConfirmDelete("Tax return.pdf")) }) {
Text("Delete")
}
The ConfirmDelete destination calls complete() when the user confirms and close() when the user cancels. See closing vs completing.
In a ViewModel
registerForNavigationResult is also an extension on ViewModel. The ViewModel extension is used as a property delegate:
class HomeViewModel : ViewModel() {
val navigation by navigationHandle<Home>()
val pickDate by registerForNavigationResult<LocalDate>(
onCompleted = { date ->
// Update a StateFlow or call a use case here.
},
)
fun onPickDateClicked() {
pickDate.open(SelectDate(maxDate = today))
}
}
The channel’s id is the destination’s instance id plus the ViewModel’s class name and the property name. A ViewModel that is recreated after process death creates a channel with the same id. The channel collects results in viewModelScope, so the callbacks stop when the ViewModel is cleared.
Result flows
- Embedded result flows — one screen opens a short sequence of result destinations, each from the
onCompletedcallback of the previous channel. - Managed result flows — a multi-step flow written as sequential code with
managedFlowDestination. - Flows in a ViewModel — a multi-step flow written as sequential code with
registerForFlowResult, displayed inside a screen’s own UI.
How results are routed
channel.open(key) stores the channel’s id in the metadata of the opened instance. When the opened instance completes or closes, Enro delivers the result to the channel with that id. A destination can register several channels. Each channel receives only the results of the instances that the channel opened.
A NavigationResultChannel has one result type. For a result that is one of several types, such as a contact or a phone number, declare a sealed class as the result type.
Forwarding a result from a synthetic destination
Several keys can declare the same result type, for example a legacy screen and a redesigned screen. A synthetic destination can choose one of the keys at runtime with completeFrom. Callers open only the synthetic destination’s key:
@Serializable object EditProfile : NavigationKey.WithResult<ProfileUpdate>
@Serializable object EditProfileLegacy : NavigationKey.WithResult<ProfileUpdate>
@Serializable object EditProfileV2 : NavigationKey.WithResult<ProfileUpdate>
@NavigationDestination(EditProfile::class)
val editProfile = syntheticDestination<EditProfile> {
if (featureFlags.useNewProfileUi) completeFrom(EditProfileV2)
else completeFrom(EditProfileLegacy)
}
completeFrom opens the chosen key and copies the result channel id from the synthetic destination’s instance to the opened instance. The result of the chosen screen’s complete(value) is delivered to the channel that opened EditProfile. See Synthetic Destinations.
See also
- Synthetic Destinations —
completeFrom. - Navigation Handles — closing vs completing.
- Returning Results recipe —
registerForNavigationResultin a Composable and in a ViewModel.