Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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]
}
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
3 changes: 3 additions & 0 deletions views/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
plugins {
alias(libs.plugins.android.library)
alias(libs.plugins.kotlin.serialization)
}

android {
Expand Down Expand Up @@ -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)
}
Original file line number Diff line number Diff line change
@@ -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<String>)

@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<NavDestination>() {
override fun createDestination() = NavDestination(this)
}
Original file line number Diff line number Diff line change
@@ -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]
}
31 changes: 31 additions & 0 deletions views/src/main/res/navigation/included_graph.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
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.
-->
<!-- [START android_views_navigation_nested_graphs_included_graph] -->
<!-- included_graph.xml -->
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/second_graph"
app:startDestination="@id/includedStart">

<fragment
android:id="@+id/includedStart"
android:name="com.example.myapplication.IncludedStart"
android:label="fragment_included_start"
tools:layout="@layout/fragment_included_start" />
</navigation>
<!-- [END android_views_navigation_nested_graphs_included_graph] -->
41 changes: 41 additions & 0 deletions views/src/main/res/navigation/nav_graph_design.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
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.
-->
<!-- [START android_views_navigation_design_nav_graph] -->
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/nav_graph"
app:startDestination="@id/profile">

<fragment
android:id="@+id/profile"
android:name="com.example.ProfileFragment"
android:label="Profile">

<!-- Action to navigate from Profile to Friends List. -->
<action
android:id="@+id/action_profile_to_friendslist"
app:destination="@id/friendslist" />
</fragment>

<fragment
android:id="@+id/friendslist"
android:name="com.example.FriendsListFragment"
android:label="Friends List" />

<!-- Add other fragment destinations similarly. -->
</navigation>
<!-- [END android_views_navigation_design_nav_graph] -->
28 changes: 28 additions & 0 deletions views/src/main/res/navigation/nav_graph_editor_destination.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
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.
-->
<!-- [START android_views_navigation_editor_destination] -->
<navigation xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
app:startDestination="@id/blankFragment">
<fragment
android:id="@+id/blankFragment"
android:name="com.example.cashdog.cashdog.BlankFragment"
android:label="@string/label_blank"
tools:layout="@layout/fragment_blank" />
</navigation>
<!-- [END android_views_navigation_editor_destination] -->
23 changes: 23 additions & 0 deletions views/src/main/res/navigation/nav_graph_editor_empty.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
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.
-->
<!-- [START android_views_navigation_editor_empty_graph] -->
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/nav_graph">

</navigation>
<!-- [END android_views_navigation_editor_empty_graph] -->
56 changes: 56 additions & 0 deletions views/src/main/res/navigation/nav_graph_nested_graphs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
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.
-->
<!-- [START android_views_navigation_nested_graphs_nested_graph] -->
<navigation xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
app:startDestination="@id/mainFragment">
<fragment
android:id="@+id/mainFragment"
android:name="com.example.cashdog.cashdog.MainFragment"
android:label="fragment_main"
tools:layout="@layout/fragment_main" >
<action
android:id="@+id/action_mainFragment_to_sendMoneyGraph"
app:destination="@id/sendMoneyGraph" />
<action
android:id="@+id/action_mainFragment_to_viewBalanceFragment"
app:destination="@id/viewBalanceFragment" />
</fragment>
<fragment
android:id="@+id/viewBalanceFragment"
android:name="com.example.cashdog.cashdog.ViewBalanceFragment"
android:label="fragment_view_balance"
tools:layout="@layout/fragment_view_balance" />
<navigation android:id="@+id/sendMoneyGraph" app:startDestination="@id/chooseRecipient">
<fragment
android:id="@+id/chooseRecipient"
android:name="com.example.cashdog.cashdog.ChooseRecipient"
android:label="fragment_choose_recipient"
tools:layout="@layout/fragment_choose_recipient">
<action
android:id="@+id/action_chooseRecipient_to_chooseAmountFragment"
app:destination="@id/chooseAmountFragment" />
</fragment>
<fragment
android:id="@+id/chooseAmountFragment"
android:name="com.example.cashdog.cashdog.ChooseAmountFragment"
android:label="fragment_choose_amount"
tools:layout="@layout/fragment_choose_amount" />
</navigation>
</navigation>
<!-- [END android_views_navigation_nested_graphs_nested_graph] -->
Loading
Loading