Google has released the new Jetpack Navigation 3 library, which redesigns from the ground up notification handling in Android apps. The new library gives full control on the back stack and integrates seamlessly with Jetpack Compose’s state management.
Jetpack Navigation 3 was created from the recognition that, although its predecessor remained perfectly functional, “the way apps are now built has fundamentally changed”.
Specifically, as Google engineer Don Turner explains, most modern apps now follow the reactive programming paradigm. This makes it possible to radically change the way developers manage their view stack. For example, with Navigation 3 you navigate to a new view by simply adding an item to a list observed by the library’s NavDisplay component. Likewise, when you want to navigate back from a screen, you remove the corresponding item from the list. NavDisplay then automatically updates the UI in response to changes in the list.
Another major advantage of adopting Jetpack state management at the cornerstone of Navigation 3 is that it makes it straightforward to establish a single source of truth. This aligns directly with the developer-defined state. In contrast, Navigation 2 maintained its own internal state, causing the app’s state to be split across the app’s own representation and Navigation 2’s.
Navigation 3 is fully modularized, offering greater flexibility and customizability through independens APIs designed to work seamlessly together. This allows developers to create complex, use case-specific functionality and deviate from the default behaviors that Navigation 3 provides for common navigation patterns. Interestingly, those defaults are implemented using the same underlying APIs. For example, developers can customize screen animations or create flexible layouts that display multiple panes at the same time.
For even greater customization, developers can replace specific Navigation 3 components with their own implementation. This is made easier by the fact that Navigation 3 code is fully available within the Android open source project (AOSP).
To use Navigation 3 in an app, developers need to associate a unique key to each displayable component, then push that key onto a back stack, and have NavDisplay observe how it changes to automatically add and remove content from the screen.
To simplify developers’ work, Google has published a repository currently featuring 19 navigation recipes, with more to come, including multiple back stacks, passing navigation arguments, returning results from screens, and more.
As a final note, Navigation 3 is also available on JetBrains’ Jetpack Compose Multiplatform. Although still in alpha, it enables non-Android apps to leverage the new navigation APIs. To better support fully native experiences, Navigation 3 multiplatform includes platform-specific enhancements, like support for edge pan gestures on iOS.
