Basic Concepts
This page defines each Enro term. The pages under Core Concepts describe each term in detail.
In Enro, a screen is declared like a function. A screen has a contract: the screenโs inputs and an optional result. Code that opens a screen depends on the contract, not on the screenโs implementation.
Navigation Keys
A NavigationKey is the contract for a screen. The properties of the NavigationKey are the screenโs inputs. A key for a screen that returns a result implements NavigationKey.WithResult<T>.
@Serializable
data class ShowProfile(val userId: String) : NavigationKey
@Serializable
data class SelectDate(
val minDate: LocalDate? = null,
val maxDate: LocalDate? = null,
) : NavigationKey.WithResult<LocalDate>
A NavigationKey must be @Serializable (kotlinx.serialization). Enro serializes each NavigationKey to save and restore the backstack. Every property of a NavigationKey must also be serializable. LocalDate in the sample is kotlinx.datetime.LocalDate.
A NavigationKey is a value and performs no navigation. A NavigationHandle opens a NavigationKey.
Navigation Destinations
A NavigationDestination is the screen that implements a contract. The @NavigationDestination(KeyClass::class) annotation binds a destination to a key. A destination has one of two forms.
A Composable function:
@Composable
@NavigationDestination(ShowProfile::class)
fun ProfileScreen() {
val navigation = navigationHandle<ShowProfile>()
Text("Profile for ${navigation.key.userId}")
}
A destination provider, created with navigationDestination<K> { }:
@NavigationDestination(ShowProfile::class)
val profileDestination = navigationDestination<ShowProfile> {
Text("Profile for ${navigation.key.userId}")
}
A destination provider can declare metadata. metadata = { dialog() } and metadata = { directOverlay() } declare a dialog, bottom sheet or overlay destination. See Navigation Destinations.
Navigation Handle
A NavigationHandle is the object a screen calls to navigate. A NavigationHandle has open, close and complete functions.
val navigation = navigationHandle<SelectDate>()
navigation.open(ShowProfile("user-123"))
navigation.close()
navigation.complete(today) // closes SelectDate and returns `today` to the caller
navigation.key is the key that opened the screen, typed as the type parameter of navigationHandle (SelectDate in the sample). complete(result) compiles only when the key type implements NavigationKey.WithResult.
Navigation Container
A NavigationContainer contains a backstack of destinations. rememberNavigationContainer creates a NavigationContainer in a Composable with an initial backstack. NavigationDisplay renders the NavigationContainer.
val container = rememberNavigationContainer(
backstack = backstackOf(Home.asInstance()),
)
NavigationDisplay(state = container)
An app usually has one root container. A destination can contain nested containers. Tabs, list-detail layouts and multiple back stacks use nested containers. See Navigation Containers.
NavigationKey.Instance
A backstack contains NavigationKey.Instance values. A NavigationKey.Instance is a key and a unique id, so a backstack can contain the same key more than once. asInstance() creates a NavigationKey.Instance from a key:
val initial = backstackOf(
Home.asInstance(),
ShowProfile("user-123").asInstance(),
)
A NavigationKey.Instance also has metadata. Metadata set on one instance applies to that instance only.
Results
A screen with a NavigationKey.WithResult<T> key returns a value to the caller. The caller registers a result channel with registerForNavigationResult and calls open on the result channel.
val getDate = registerForNavigationResult<LocalDate>(
onCompleted = { date -> /* use date */ },
)
Button(onClick = { getDate.open(SelectDate(maxDate = today)) }) {
Text("Pick a date")
}
The SelectDate screen returns the value with navigation.complete(date). See Results.
NavigationComponent
A NavigationComponent configures Enro for an application. An application declares one NavigationComponent, annotated with @NavigationComponent, and installs the NavigationComponent when the application starts.
@NavigationComponent
object MyComponent : NavigationComponentConfiguration(
module = createNavigationModule { /* optional config */ }
)
See Installation.
Structure of an Enro app
Application starts
โโโ MyComponent.installNavigationController(this) โ called once
Compose tree
โโโ rememberNavigationContainer(backstack = ...) โ NavigationContainer
โโโ NavigationDisplay(state = container)
โโโ Destination for the current key on the stack
โโโ navigationHandle<MyKey>() โ NavigationHandle
โโโ .open(...) / .close() / .complete(...)
Next steps
- Your First Screen builds an example with three screens.
- The Core Concepts pages describe each term on this page in detail.