Skip to content

Conversation

@dturner
Copy link
Collaborator

@dturner dturner commented Nov 7, 2025

Add a new recipe to show how to implement multiple back stacks where state is retained for each back stack when switching between them. This also demonstrates the use of the decorator APIs.

Implementation details:

The Navigator class encapsulates the logic for maintaining separate back stacks for each top-level route and handles state persistence through a custom Saver. It exposes a Composable entries() function to provide the NavEntry list for NavDisplay.

This commit refactors the common UI recipe to use a new `Navigator` class for managing navigation state, specifically for handling multiple back stacks.

The `Navigator` class encapsulates the logic for maintaining separate back stacks for each top-level route and handles state persistence through a custom `Saver`. It exposes a Composable `entries()` function to provide the `NavEntry` list for `NavDisplay`.

Key changes:
- `Navigator.kt`: New class to manage navigation state, including stacks for top-level routes and navigation actions (`navigate`, `goBack`).
- `CommonUiActivity.kt`: Refactored to use the `rememberNavigator` composable. The previous custom `TopLevelBackStack` implementation has been removed.
- `Content.kt`: New file containing composable `entryProvider` extensions (`featureASection`, `featureBSection`, etc.) to define the content for different routes.
- `README.md`: Added a README for the `commonui` recipe explaining the new implementation using the `Navigator` class.
This commit removes unused imports from `CommonUiActivity`.
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request does a great job of refactoring the common UI recipe to demonstrate multiple back stacks using a new Navigator class. The separation of concerns within the Navigator is clean, and the use of a Saver to persist state is a key improvement. My review includes a fix for a navigation bug and a couple of suggestions to improve the code's clarity and efficiency in the new Navigator implementation.

dturner and others added 6 commits November 7, 2025 18:08
The "Common navigation UI" recipe has been renamed to "Multiple back stacks / Common navigation UI" to better reflect its functionality. The description has been expanded to clarify that it demonstrates creating multiple top-level routes, each with its own back stack and retained state.
…ivity.kt

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
The `Navigator` class has been refactored to make its state restoration logic more robust and to improve its testability.

The primary constructor is now private and a new public secondary constructor has been added. This change allows for the injection of initial state (`initialTopLevelRoute` and `initialTopLevelStacks`) during instantiation, which simplifies state restoration from a `SavedState` and makes the class easier to unit test. The state restoration logic within the `Saver` has been updated to use this new constructor, streamlining the process.

Additionally, a bug in `CommonUiActivity` where the "Feature C" section incorrectly navigated to `RouteA1` has been fixed to correctly navigate to `RouteC1`.
@dturner dturner changed the title Update the CommonUi recipe to be the canonical example for multiple back stacks. Add multiple back stacks recipe Nov 8, 2025
@android android deleted a comment from gemini-code-assist bot Nov 8, 2025
The `topLevelRoutes` property is removed from the `Navigator` constructor. This information is now derived directly from the keys of the `initialTopLevelStacks` map.
This commit refactors the multiple back stacks recipe to leverage the new state management APIs from `alpha11`.

Key changes:
*   The custom `Navigator` class, which previously managed its own state and persistence logic via a `Saver`, has been removed.
*   The `NavigationState` class is introduced to hold the navigation state, created using `rememberNavigationState`.
*   State persistence is now handled by `rememberSerializable` for the current top-level route and `rememberNavBackStack` for each of the back stacks.
*   The custom `Route` sealed class has been replaced with the standard `NavKey` interface.
*   The `Navigator` class is now a simpler, stateless handler for navigation events, which operates on the `NavigationState`.
*   The README has been updated to reflect these architectural changes.
Rename "Multiple back stacks / Common navigation UI" recipe to "Common navigation UI".
The description for the Common UI recipe was incorrectly copied from the Multiple back stacks recipe. This commit updates the README to accurately describe the Common UI recipe.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants