Migrating from Enro 2
Enro 3 is a rewrite of Enro 2. A screen is still a NavigationKey with typed inputs and an optional typed result, bound to a destination. Most APIs have a new name or a new signature, and Enro 3 is a Kotlin Multiplatform library built on Compose.
This guide lists each API change with Enro 2 and Enro 3 code, and ends with one screen converted from Enro 2 to Enro 3.
An Enro 2 app with Fragment or Activity destinations can migrate one screen at a time by depending on enro-compat. enro-compat provides the Enro 2 API on top of Enro 3. See the Android platform guide.
Summary of changes
- Kotlin Multiplatform. Enro 3 targets Android, iOS, Desktop and Web through Compose Multiplatform. The common API does not use Parcelize, Fragments or Activities.
- Compose destinations. A Composable function annotated with
@NavigationDestinationis the default destination type. Fragment and Activity destinations are supported on Android only. @Parcelize→@Serializable. Enro 3 serializes navigation keys with kotlinx.serialization on every platform.- Push and Present are removed. A
NavigationKeydoes not declare how the destination is presented. Destination metadata declares the presentation (a dialog, for example), and a scene strategy renders the destination according to the metadata. NavigationApplicationis removed. The app callsinstallNavigationControllerfromApplication.onCreate, or from the entry point of each other platform.NavigationInstructionis renamedNavigationOperation. The operations areOpen,Close,Complete,CompleteFrom,SetBackstack,SideEffectandAggregateOperation.- KSP replaces kapt. The processor is
enro-processor, applied with thecom.google.devtools.kspGradle plugin.
API changes
| Enro 2 | Enro 3 |
|---|---|
NavigationKey.SupportsPush, SupportsPresent, SupportsPresent.WithResult<T> | NavigationKey, NavigationKey.WithResult<T> |
@Parcelize class Key(...) : NavigationKey.SupportsPush | @Serializable data class Key(...) : NavigationKey |
navigation.push(key) / navigation.present(key) | navigation.open(key) |
navigation.closeWithResult(result) | navigation.complete(result) |
class MyApp : Application(), NavigationApplication { override val navigationController = ... } | MyComponent.installNavigationController(this) inside Application.onCreate |
NavigationController (property on Application) | EnroController, returned by installNavigationController |
NavigationInstruction.Open / .Close / .RequestClose | NavigationOperation.Open / .Close / .Complete / .SetBackstack / etc. |
NavigationExecutor | Removed. Scene strategies render destinations. |
NavigationBinding (registered manually or by generated code) | NavigationBinding<K>, generated by KSP from @NavigationDestination annotations. |
enroViewModels<MyVm>() property delegate | viewModel { createEnroViewModel { MyVm() } } in a Composable. by navigationHandle<MyKey>() inside the ViewModel is unchanged. |
kapt("dev.enro:enro-processor:...") | ksp("dev.enro:enro-processor:...") |
composableDestination<Key> { MyScreen() } (in the module DSL) | @NavigationDestination(Key::class) on a Composable, or val foo = navigationDestination<Key> { ... } with destination(foo) in createNavigationModule. |
composeEnvironment { content -> ... } in a module | Deprecated. Use decorator { navigationDestinationDecorator { ... } }. |
syntheticDestination { sendResult(value) } | syntheticDestination { complete(value) }. See Synthetic Destinations. enro-compat contains sendResult and forwardResult as @Deprecated extensions that name the replacement. |
syntheticDestination { forwardResult(otherKey) } | syntheticDestination { completeFrom(otherKey) } |
EnroTestRule (JUnit) | EnroTestRule on Android. runEnroTest { ... } on every platform. |
EnroPreview(key) { ... } | EnroPreview in enro-compat. EnroPreview renders the content without a navigation handle. |
navigationHandle.onContainer { }, onParentContainer { }, onActiveContainer { } | Deprecated in enro-compat. enro-compat never runs the block. Get the NavigationContainer from the navigation context and update the container’s backstack. |
expectOpenInstruction<T>(), expectCloseInstruction() test helpers | TestNavigationHandle<T> with assertOpened, assertClosed, assertCompleted, assertOperationExecuted. enro-test contains the Enro 2 helpers on Android only. |
Detailed changes
Navigation keys
An Enro 2 key declares the presentation in the key’s supertype:
@Parcelize data class ShowProfile(val userId: String) : NavigationKey.SupportsPush
@Parcelize data class SelectDate(val min: LocalDate?) : NavigationKey.SupportsPresent.WithResult<LocalDate>
An Enro 3 key declares only the inputs and, with NavigationKey.WithResult<T>, the result type:
@Serializable data class ShowProfile(val userId: String) : NavigationKey
@Serializable data class SelectDate(val min: LocalDate?) : NavigationKey.WithResult<LocalDate>
Enro 2 call sites chose the presentation with navigation.push(...) or navigation.present(...). In Enro 3 the destination’s metadata sets the presentation. A destination with dialog() metadata is rendered as a dialog:
@NavigationDestination(SelectDate::class)
val selectDateDestination = navigationDestination<SelectDate>(
metadata = { dialog() }
) {
// ...
}
Navigation Destinations describes destination metadata.
NavigationHandle operations
| Enro 2 | Enro 3 |
|---|---|
navigation.push(key) | navigation.open(key) |
navigation.present(key) | navigation.open(key) (with dialog() / directOverlay() metadata on the destination) |
navigation.close() | navigation.close() (unchanged) |
navigation.requestClose() | navigation.requestClose() (unchanged) |
navigation.closeWithResult(value) | navigation.complete(value) |
navigation.execute(NavigationInstruction.Open(...)) | navigation.execute(NavigationOperation.Open(...)) |
The extension functions are declared in package dev.enro: import dev.enro.open, dev.enro.close, dev.enro.complete and dev.enro.requestClose.
In Enro 3, close() removes the destination from the destination’s container immediately. A destination that has been removed from the container ignores later operations. Enro 2 code that calls close() and then open(key) on the same handle therefore does not open key. Use closeAndReplaceWith(key), which closes the destination and opens key in one operation:
// Enro 2
navigation.close()
navigation.push(NextScreen())
// Enro 3
navigation.closeAndReplaceWith(NextScreen())
Installing the controller
Enro 2:
@NavigationComponent
class MyApp : Application(), NavigationApplication {
override val navigationController = installNavigationController {
// configuration
}
}
Enro 3:
@NavigationComponent
object MyComponent : NavigationComponentConfiguration(
module = createNavigationModule { /* optional config */ }
)
class MyApp : Application() {
override fun onCreate() {
super.onCreate()
MyComponent.installNavigationController(this)
}
}
Enro 3 has no NavigationApplication interface, and the Application class does not declare a controller property. The same component object installs the controller on the other platforms: installNavigationController(document) on Web, installNavigationController(Unit) on Desktop and installNavigationController(application:) on iOS.
Module DSL
createNavigationModule { } replaces the Enro 2 module DSL (override fun NavigationModuleScope.configure()). Pass the module to the NavigationComponentConfiguration constructor. The module builder has these functions:
createNavigationModule {
plugin(MyPlugin())
interceptor { /* NavigationInterceptorBuilder */ }
decorator { navigationDestinationDecorator { /* wrap every destination */ } }
destination(myProviderVal) // for navigationDestination val { } providers
path(MyDeepLinkPathBinding)
serializersModule(myKotlinxSerializersModule)
module(otherFeatureModule) // compose modules from other libraries
}
composeEnvironment { content -> ... } is deprecated. Use decorator { }.
Rendering a backstack
Enro 2:
val container = rememberNavigationContainer(
root = HomeKey,
emptyBehavior = EmptyBehavior.CloseParent,
)
Enro 3:
val container = rememberNavigationContainer(
backstack = backstackOf(Home.asInstance()),
)
NavigationDisplay(state = container)
asInstance() wraps a key in a NavigationKey.Instance. Each NavigationKey.Instance in a backstack has its own id and metadata.
NavigationDisplay renders a container. NavigationDisplay replaces the Enro 2 Container { } Composable.
With enro-compat on Android, Enro 2 code that calls container.Render() compiles after the file imports the enro-compat extension:
import dev.enro.core.compose.container.Render
container.Render() // deprecated: renders NavigationDisplay(container)
In Enro 2, Render() is a member of the container, so Enro 2 call sites have no import for Render. The compiler reports “Unresolved reference: Render” until the import is added. The Render() extension is deprecated. Replace container.Render() with NavigationDisplay(state = container) before removing enro-compat.
Results
Enro 2:
class HomeScreen : Fragment() {
val getDate by registerForNavigationResult<LocalDate> { date -> /* use the date */ }
fun onPickDate() = getDate.present(SelectDate())
}
@NavigationDestination(SelectDate::class)
class SelectDateScreen : Fragment() {
val navigation by navigationHandle<SelectDate>()
fun onPicked(date: LocalDate) = navigation.closeWithResult(date)
}
Enro 3:
@Composable
@NavigationDestination(Home::class)
fun HomeScreen() {
val getDate = registerForNavigationResult<LocalDate>(
onCompleted = { date -> /* use the date */ },
onClosed = { /* dismissed without a result */ },
)
Button(onClick = { getDate.open(SelectDate()) }) { Text("Pick a date") }
}
@Composable
@NavigationDestination(SelectDate::class)
fun SelectDateScreen() {
val navigation = navigationHandle<SelectDate>()
// ...
Button(onClick = { navigation.complete(today) }) { Text("Use today") }
}
In a Composable, registerForNavigationResult returns a NavigationResultChannel<T> and is not used with by. In a ViewModel, registerForNavigationResult is a property delegate, as in Enro 2.
ViewModels
Enro 2 creates the ViewModel with the enroViewModels delegate:
class ProfileViewModel : ViewModel() {
val navigation by navigationHandle<ShowProfile>()
}
@NavigationDestination(ShowProfile::class)
class ProfileFragment : Fragment() {
val viewModel by enroViewModels<ProfileViewModel>()
}
Enro 3 creates the ViewModel with the AndroidX viewModel { } function. createEnroViewModel { } provides the destination’s NavigationHandle to the ViewModel:
class ProfileViewModel : ViewModel() {
val navigation by navigationHandle<ShowProfile>()
}
@Composable
@NavigationDestination(ShowProfile::class)
fun ProfileScreen() {
val viewModel = viewModel { createEnroViewModel { ProfileViewModel() } }
// ...
}
The by navigationHandle<MyKey>() delegate inside the ViewModel is unchanged.
Testing
EnroTestRule is available for JUnit tests on Android:
class ProfileTest {
@get:Rule(order = 0) val enroRule = EnroTestRule()
@get:Rule(order = 1) val composeRule = createComposeRule()
@Test fun example() { /* ... */ }
}
runEnroTest { } is available on every platform and does not require a JUnit rule:
@Test fun example() = runEnroTest {
val handle = createTestNavigationHandle(ShowProfile("user-123"))
handle.open(SelectDate(min = null))
handle.assertOpened<SelectDate>()
}
TestNavigationHandle<T> records every NavigationOperation executed on the handle. The assertions are assertOpened, assertClosed, assertCompleted and assertOperationExecuted.
putNavigationHandleForViewModel<MyVm, MyKey>(key) is unchanged.
See Testing.
Worked example
An Enro 2 screen:
@Parcelize
data class GreetUser(val name: String) : NavigationKey.SupportsPresent.WithResult<String>
@NavigationDestination(GreetUser::class)
class GreetUserFragment : Fragment() {
val navigation by navigationHandle<GreetUser>()
override fun onCreateView(...): View {
return ComposeView(requireContext()).apply {
setContent {
Column {
Text("Hello, ${navigation.key.name}!")
Button(onClick = { navigation.closeWithResult("greeted") }) {
Text("Done")
}
}
}
}
}
}
The same screen in Enro 3 is a Composable destination. dialog() metadata replaces SupportsPresent:
@Serializable
data class GreetUser(val name: String) : NavigationKey.WithResult<String>
@NavigationDestination(GreetUser::class)
val greetUserDestination = navigationDestination<GreetUser>(
metadata = { dialog() } // <-- presentation moves here
) {
Column {
Text("Hello, ${navigation.key.name}!")
Button(onClick = { navigation.complete("greeted") }) {
Text("Done")
}
}
}
Callers replace push and present with open. The destination replaces closeWithResult with complete. The key’s inputs and result type are unchanged.
Unchanged APIs
@NavigationDestination(KeyClass::class)binds a screen to a key.by navigationHandle<MyKey>()provides theNavigationHandleinside aViewModel.- Result channels are typed.
onCompletedis called once with the result. - Backstacks are serializable and are restored after process death on Android.
- Each module that declares a
@NavigationDestinationrequires theenro-processorKSP dependency, and the app module depends on each of those modules directly or transitively.
Help
- The recipes module contains runnable examples of the Enro 3 APIs.
- The troubleshooting page lists common errors.
- Report a problem that this guide does not cover at github.com/isaac-udy/Enro/issues.