diff --git a/compose/snippets/src/main/java/com/example/compose/snippets/navigation/design/DesignGraph.kt b/compose/snippets/src/main/java/com/example/compose/snippets/navigation/design/DesignGraph.kt new file mode 100644 index 000000000..41d3dc667 --- /dev/null +++ b/compose/snippets/src/main/java/com/example/compose/snippets/navigation/design/DesignGraph.kt @@ -0,0 +1,33 @@ +/* + * Copyright 2026 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.compose.snippets.navigation.design + +import kotlinx.serialization.Serializable + +private object RouteWithArgument { + // [START android_compose_navigation_design_arguments] + @Serializable + data class Profile(val name: String) + // [END android_compose_navigation_design_arguments] +} + +private object RouteWithOptionalArgument { + // [START android_compose_navigation_design_optional_arguments] + @Serializable + data class Profile(val nickname: String? = null) + // [END android_compose_navigation_design_optional_arguments] +} diff --git a/compose/snippets/src/main/java/com/example/compose/snippets/navigation/nestedgraphs/NestedGraphs.kt b/compose/snippets/src/main/java/com/example/compose/snippets/navigation/nestedgraphs/NestedGraphs.kt new file mode 100644 index 000000000..7240239bf --- /dev/null +++ b/compose/snippets/src/main/java/com/example/compose/snippets/navigation/nestedgraphs/NestedGraphs.kt @@ -0,0 +1,29 @@ +/* + * Copyright 2026 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.compose.snippets.navigation.nestedgraphs + +import androidx.navigation.NavController +import kotlinx.serialization.Serializable + +private fun navigateToNestedDestination(navController: NavController) { + // [START android_compose_navigation_nested_graphs_navigate] + navController.navigate(route = Match) + // [END android_compose_navigation_nested_graphs_navigate] +} + +@Serializable +private object Match diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 6cb8bc0ed..34a360f10 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -247,6 +247,7 @@ androidx-media3-ui = { module = "androidx.media3:media3-ui", version.ref = "medi androidx-media3-ui-compose = { module = "androidx.media3:media3-ui-compose", version.ref = "media3Ui" } androidx-media3-ui-compose-material3 = { module = "androidx.media3:media3-ui-compose-material3", version.ref = "media3Ui" } androidx-navigation-compose = { module = "androidx.navigation:navigation-compose", version.ref = "androidx-navigation" } +androidx-navigation-fragment-ktx = { module = "androidx.navigation:navigation-fragment-ktx", version.ref = "androidx-navigation" } androidx-navigation3-runtime = { module = "androidx.navigation3:navigation3-runtime", version.ref = "androidx-navigation3" } androidx-navigation3-ui = { module = "androidx.navigation3:navigation3-ui", version.ref = "androidx-navigation3" } androidx-paging-common = { module = "androidx.paging:paging-common", version.ref = "androidx-paging" } diff --git a/views/build.gradle.kts b/views/build.gradle.kts index 70c073cc4..c62b7d61c 100644 --- a/views/build.gradle.kts +++ b/views/build.gradle.kts @@ -16,6 +16,7 @@ import org.jetbrains.kotlin.gradle.dsl.JvmTarget plugins { alias(libs.plugins.android.library) + alias(libs.plugins.kotlin.serialization) } android { @@ -57,4 +58,6 @@ dependencies { implementation(libs.androidx.glance.appwidget) implementation(libs.androidx.webkit) implementation(libs.kotlinx.coroutines.jdk9) + implementation(libs.androidx.navigation.fragment.ktx) + implementation(libs.kotlinx.serialization.core) } diff --git a/views/src/main/java/com/example/example/snippet/views/navigation/kotlindsl/KotlinDsl.kt b/views/src/main/java/com/example/example/snippet/views/navigation/kotlindsl/KotlinDsl.kt new file mode 100644 index 000000000..b6faaf528 --- /dev/null +++ b/views/src/main/java/com/example/example/snippet/views/navigation/kotlindsl/KotlinDsl.kt @@ -0,0 +1,89 @@ +/* + * Copyright 2026 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.example.snippet.views.navigation.kotlindsl + +import androidx.fragment.app.Fragment +import androidx.navigation.NavController +import androidx.navigation.NavDestination +import androidx.navigation.Navigator +import androidx.navigation.createGraph +import androidx.navigation.fragment.findNavController +import androidx.navigation.get +import kotlinx.serialization.Serializable + +// [START android_views_navigation_kotlin_dsl_routes] +@Serializable data object Home +@Serializable data class Plant(val id: String) +// [END android_views_navigation_kotlin_dsl_routes] + +class HomeFragment : Fragment() { + // [START android_views_navigation_kotlin_dsl_navigate] + private fun navigateToPlant(plantId: String) { + findNavController().navigate(route = PlantDetail(id = plantId)) + } + // [END android_views_navigation_kotlin_dsl_navigate] +} + +private fun NavController.addCustomDestination() { + graph = createGraph(startDestination = Graph.CustomDestination.route) { + // [START android_views_navigation_kotlin_dsl_custom_destination] + // The NavigatorProvider is retrieved from the NavController. + val customDestination = navigatorProvider[CustomNavigator::class].createDestination().apply { + route = Graph.CustomDestination.route + } + addDestination(customDestination) + // [END android_views_navigation_kotlin_dsl_custom_destination] + } +} + +private fun NavController.addCustomDestinationWithUnaryPlus() { + graph = createGraph(startDestination = Graph.CustomDestination.route) { + // [START android_views_navigation_kotlin_dsl_custom_destination_unary_plus] + // The NavigatorProvider is retrieved from the NavController. + +navigatorProvider[CustomNavigator::class].createDestination().apply { + route = Graph.CustomDestination.route + } + // [END android_views_navigation_kotlin_dsl_custom_destination_unary_plus] + } +} + +private fun navigateToSearch(navController: NavController) { + // [START android_views_navigation_kotlin_dsl_custom_type_navigate] + val params = SearchParameters("rose", listOf("available")) + navController.navigate(route = SearchRoute(params)) + // [END android_views_navigation_kotlin_dsl_custom_type_navigate] +} + +@Serializable +private data class PlantDetail(val id: String) + +@Serializable +private data class SearchParameters(val searchQuery: String, val filters: List) + +@Serializable +private data class SearchRoute(val parameters: SearchParameters) + +private object Graph { + object CustomDestination { + const val route = "custom_destination" + } +} + +@Navigator.Name("custom") +private class CustomNavigator : Navigator() { + override fun createDestination() = NavDestination(this) +} diff --git a/views/src/main/java/com/example/example/snippet/views/navigation/nestedgraphs/NestedGraphs.kt b/views/src/main/java/com/example/example/snippet/views/navigation/nestedgraphs/NestedGraphs.kt new file mode 100644 index 000000000..8810dcd1f --- /dev/null +++ b/views/src/main/java/com/example/example/snippet/views/navigation/nestedgraphs/NestedGraphs.kt @@ -0,0 +1,27 @@ +/* + * Copyright 2026 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.example.snippet.views.navigation.nestedgraphs + +import android.view.View +import androidx.navigation.findNavController +import com.example.example.snippet.views.R + +private fun navigateToNestedGraph(view: View) { + // [START android_views_navigation_nested_graphs_navigate_action] + view.findNavController().navigate(R.id.action_mainFragment_to_sendMoneyGraph) + // [END android_views_navigation_nested_graphs_navigate_action] +} diff --git a/views/src/main/res/navigation/included_graph.xml b/views/src/main/res/navigation/included_graph.xml new file mode 100644 index 000000000..67cae8162 --- /dev/null +++ b/views/src/main/res/navigation/included_graph.xml @@ -0,0 +1,31 @@ + + + + + + + + + diff --git a/views/src/main/res/navigation/nav_graph_design.xml b/views/src/main/res/navigation/nav_graph_design.xml new file mode 100644 index 000000000..f6b1c8d00 --- /dev/null +++ b/views/src/main/res/navigation/nav_graph_design.xml @@ -0,0 +1,41 @@ + + + + + + + + + + + + + + + + diff --git a/views/src/main/res/navigation/nav_graph_editor_destination.xml b/views/src/main/res/navigation/nav_graph_editor_destination.xml new file mode 100644 index 000000000..e190384c5 --- /dev/null +++ b/views/src/main/res/navigation/nav_graph_editor_destination.xml @@ -0,0 +1,28 @@ + + + + + + + diff --git a/views/src/main/res/navigation/nav_graph_editor_empty.xml b/views/src/main/res/navigation/nav_graph_editor_empty.xml new file mode 100644 index 000000000..25cf44802 --- /dev/null +++ b/views/src/main/res/navigation/nav_graph_editor_empty.xml @@ -0,0 +1,23 @@ + + + + + + + diff --git a/views/src/main/res/navigation/nav_graph_nested_graphs.xml b/views/src/main/res/navigation/nav_graph_nested_graphs.xml new file mode 100644 index 000000000..71364d740 --- /dev/null +++ b/views/src/main/res/navigation/nav_graph_nested_graphs.xml @@ -0,0 +1,56 @@ + + + + + + + + + + + + + + + + + diff --git a/views/src/main/res/navigation/nav_graph_nested_graphs_include.xml b/views/src/main/res/navigation/nav_graph_nested_graphs_include.xml new file mode 100644 index 000000000..92e13e332 --- /dev/null +++ b/views/src/main/res/navigation/nav_graph_nested_graphs_include.xml @@ -0,0 +1,39 @@ + + + + + + + + + + + + + + + diff --git a/views/src/main/res/values/strings.xml b/views/src/main/res/values/strings.xml index 953715855..f7146543b 100644 --- a/views/src/main/res/values/strings.xml +++ b/views/src/main/res/values/strings.xml @@ -19,4 +19,5 @@ Email sent Email archived Undo + Blank