-
Notifications
You must be signed in to change notification settings - Fork 70
Add multiple back stacks recipe #121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
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`.
There was a problem hiding this 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.
app/src/main/java/com/example/nav3recipes/commonui/CommonUiActivity.kt
Outdated
Show resolved
Hide resolved
app/src/main/java/com/example/nav3recipes/commonui/Navigator.kt
Outdated
Show resolved
Hide resolved
app/src/main/java/com/example/nav3recipes/commonui/Navigator.kt
Outdated
Show resolved
Hide resolved
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`.
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.
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
Navigatorclass encapsulates the logic for maintaining separate back stacks for each top-level route and handles state persistence through a customSaver. It exposes a Composableentries()function to provide theNavEntrylist forNavDisplay.