Skip to content

Commit 4e268ac

Browse files
authored
Merge pull request #126 from android/dt/add-readmes
Add READMEs for each recipe.
2 parents f3ab73b + fe3d03d commit 4e268ac

36 files changed

+280
-131
lines changed

app/src/main/java/com/example/nav3recipes/animations/AnimatedActivity.kt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,6 @@ import com.example.nav3recipes.ui.setEdgeToEdgeConfig
2525
import kotlinx.serialization.Serializable
2626

2727

28-
/**
29-
* This recipe shows how to override the default animations at the `NavDisplay` level, and at the
30-
* individual destination level, shown for `ScreenC`.
31-
*
32-
*/
3328
@Serializable
3429
private data object ScreenA : NavKey
3530

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Animations Recipe
2+
3+
This recipe shows how to override the default animations at the `NavDisplay` level, and at the individual destination level.
4+
5+
## How it works
6+
7+
The `NavDisplay` composable takes `transitionSpec`, `popTransitionSpec`, and `predictivePopTransitionSpec` parameters to define the animations for forward, backward, and predictive back navigation respectively. These animations will be applied to all destinations by default.
8+
9+
In this example, we use `slideInHorizontally` and `slideOutHorizontally` to create a sliding animation for forward and backward navigation.
10+
11+
It is also possible to override these animations for a specific destination by providing a different `transitionSpec` and `popTransitionSpec` to the `entry` composable. In this recipe, `ScreenC` has a custom vertical slide animation.

app/src/main/java/com/example/nav3recipes/basic/BasicActivity.kt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,6 @@ import com.example.nav3recipes.content.ContentBlue
2929
import com.example.nav3recipes.content.ContentGreen
3030
import com.example.nav3recipes.ui.setEdgeToEdgeConfig
3131

32-
/**
33-
* Basic example with two screens, showing how to use the Navigation 3 API.
34-
*/
35-
3632
private data object RouteA
3733

3834
private data class RouteB(val id: String)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Basic Recipe
2+
3+
This recipe shows a basic example of how to use the Navigation 3 API with two screens.
4+
5+
## How it works
6+
7+
This example defines two routes: `RouteA` and `RouteB`. `RouteA` is a `data object` representing a simple screen, while `RouteB` is a `data class` that takes an `id` as a parameter.
8+
9+
A `mutableStateListOf<Any>` is used to manage the navigation back stack.
10+
11+
The `NavDisplay` composable is used to display the current screen. Its `entryProvider` parameter is a lambda that takes a route from the back stack and returns a `NavEntry`. Inside the `entryProvider`, a `when` statement is used to determine which composable to display based on the route.
12+
13+
To navigate from `RouteA` to `RouteB`, we simply add a `RouteB` instance to the back stack. The `id` is passed as an argument to the `RouteB` data class.

app/src/main/java/com/example/nav3recipes/basicdsl/BasicDslActivity.kt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,6 @@ import com.example.nav3recipes.content.ContentGreen
3030
import com.example.nav3recipes.ui.setEdgeToEdgeConfig
3131
import kotlinx.serialization.Serializable
3232

33-
/**
34-
* Basic example with two screens that uses the entryProvider DSL and has a persistent back stack.
35-
*/
36-
3733
@Serializable
3834
private data object RouteA : NavKey
3935

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Basic DSL Recipe
2+
3+
This recipe shows a basic example of how to use the Navigation 3 API with two screens, using the `entryProvider` DSL and a persistent back stack.
4+
5+
## How it works
6+
7+
This example is similar to the basic recipe, but with a few key differences:
8+
9+
1. **Persistent Back Stack**: It uses `rememberNavBackStack(RouteA)` to create and remember the back stack. This makes the back stack persistent across configuration changes (e.g., screen rotation). To use `rememberNavBackStack`, the navigation keys must be serializable, which is why `RouteA` and `RouteB` are annotated with `@Serializable` and implement the `NavKey` interface.
10+
11+
2. **`entryProvider` DSL**: Instead of a `when` statement, this example uses the `entryProvider` DSL to define the content for each route. The `entry<RouteType>` function is used to associate a route type with its composable content.
12+
13+
The navigation logic remains the same: to navigate from `RouteA` to `RouteB`, we add a `RouteB` instance to the back stack.

app/src/main/java/com/example/nav3recipes/basicsaveable/BasicSaveableActivity.kt

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,6 @@ import com.example.nav3recipes.content.ContentGreen
3030
import com.example.nav3recipes.ui.setEdgeToEdgeConfig
3131
import kotlinx.serialization.Serializable
3232

33-
/**
34-
* Basic example with a persistent back stack state.
35-
*
36-
* The back stack persists config changes because it's created using `rememberNavBackStack`. This
37-
* requires that the back stack keys be both serializable and implement `NavKey`.
38-
*/
39-
4033
@Serializable
4134
private data object RouteA : NavKey
4235

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Basic Saveable Recipe
2+
3+
This recipe shows a basic example of how to create a persistent back stack that survives configuration changes.
4+
5+
## How it works
6+
7+
To make the back stack persistent, we use the `rememberNavBackStack` function. This function creates and remembers the back stack across configuration changes (e.g., screen rotation).
8+
9+
A requirement for using `rememberNavBackStack` is that the navigation keys (routes) must be serializable. In this example, `RouteA` and `RouteB` are annotated with `@Serializable` and implement the `NavKey` interface.
10+
11+
This example uses a `when` statement within the `entryProvider` to map routes to their corresponding composables, but it could also be used with the `entryProvider` DSL.

app/src/main/java/com/example/nav3recipes/bottomsheet/BottomSheetActivity.kt

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,6 @@ import com.example.nav3recipes.content.ContentGreen
3636
import com.example.nav3recipes.ui.setEdgeToEdgeConfig
3737
import kotlinx.serialization.Serializable
3838

39-
/**
40-
* This recipe demonstrates how to create a bottom sheet. It does this by:
41-
*
42-
* - Adding the `BottomSheetSceneStrategy` to the list of strategies used by `NavDisplay`.
43-
* - Adding `BottomSheetSceneStrategy.bottomSheet()` to a `NavEntry`'s metadata to indicate that it
44-
* is a bottom sheet. In this case it is applied to the `NavEntry` for `RouteB`.
45-
*
46-
* See also https://developer.android.com/guide/navigation/navigation-3/custom-layouts
47-
*/
48-
4939
@Serializable
5040
private data object RouteA : NavKey
5141

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Bottom Sheet Recipe
2+
3+
This recipe demonstrates how to display a destination as a modal bottom sheet.
4+
5+
## How it works
6+
7+
To show a destination as a bottom sheet, you need to do two things:
8+
9+
1. **Use `BottomSheetSceneStrategy`**: Create an instance of `BottomSheetSceneStrategy` and pass it to the `sceneStrategy` parameter of the `NavDisplay` composable.
10+
11+
2. **Add metadata to the destination**: For the destination that you want to display as a bottom sheet, add `BottomSheetSceneStrategy.bottomSheet()` to its metadata. This is done in the `entry` function.
12+
13+
In this example, `RouteB` is configured to be a bottom sheet. When you navigate from `RouteA` to `RouteB`, `RouteB` will be displayed in a modal bottom sheet that slides up from the bottom of the screen.
14+
15+
The content of the bottom sheet can be styled as needed. In this recipe, the content is clipped to have rounded corners.
16+
17+
For more information, see the official documentation on [custom layouts](https://developer.android.com/guide/navigation/navigation-3/custom-layouts).

0 commit comments

Comments
 (0)