開始使用
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
如要開始使用 Navigation 3,請將程式庫與任何支援的程式庫一併新增至專案。請參考下表,決定要新增哪些程式庫。
成果
名稱 |
功能 |
Artifact |
Navigation 3 執行階段程式庫 |
Core Navigation 3 API。包含 NavEntry, EntryProvider 和相關的 DSL。 |
androidx.navigation3:navigation3-runtime |
Navigation 3 UI 程式庫 |
提供用於顯示內容的類別,包括 NavDisplay 和 Scene 。 |
androidx.navigation3:navigation3-ui |
Navigation 3 的 ViewModel 生命週期 |
允許 ViewModel 的範圍限定在返回堆疊中的項目。 |
androidx.lifecycle:lifecycle-viewmodel-navigation3 |
Navigation 3 適用的 Material 3 自動調整式版面配置 |
提供可與 NavDisplay 搭配使用的自適應版面配置 (SceneStrategies、Scenes 和中繼資料定義)。 |
androidx.compose.material3.adaptive:adaptive-navigation3 適用於快照版本 |
KotlinX 序列化 |
允許序列化導覽鍵。 |
外掛程式:org.jetbrains.kotlin.plugin.serialization 媒體庫: org.jetbrains.kotlinx:kotlinx-serialization-core |
專案設定
如要在現有專案中新增 Navigation 3 程式庫,請在 libs.versions.toml
中加入下列內容:
[versions]
nav3Core = "1.0.0-alpha01"
lifecycleViewmodelNav3 = "1.0.0-alpha01"
kotlinSerialization = "2.1.21"
kotlinxSerializationCore = "1.8.1"
material3AdaptiveNav3 = "1.0.0-SNAPSHOT"
[libraries]
# Core Navigation 3 libraries
androidx-navigation3-runtime = { module = "androidx.navigation3:navigation3-runtime", version.ref = "nav3Core" }
androidx-navigation3-ui = { module = "androidx.navigation3:navigation3-ui", version.ref = "nav3Core" }
# Optional add-on libraries
androidx-lifecycle-viewmodel-navigation3 = { module = "androidx.lifecycle:lifecycle-viewmodel-navigation3", version.ref = "lifecycleViewmodelNav3" }
kotlinx-serialization-core = { module = "org.jetbrains.kotlinx:kotlinx-serialization-core", version.ref = "kotlinxSerializationCore" }
# Note: The Material3 adaptive layouts library for Navigation 3 is currently
# only available in snapshot builds. Follow the instructions at androidx.dev to
# add the snapshot builds repository to your project.
androidx-material3-adaptive-navigation3 = { group = "androidx.compose.material3.adaptive", name = "adaptive-navigation3", version.ref = "material3AdaptiveNav3" }
[plugins]
# Optional plugins
jetbrains-kotlin-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlinSerialization"}
此外,請將編譯 SDK 更新至 36 以上版本:
[versions]
compileSdk = "36"
將以下內容新增至 app 建構檔案 app/build.gradle.kts
:
plugins {
...
// Optional, provides the @Serialize annotation for autogeneration of Serializers.
alias(libs.plugins.jetbrains.kotlin.serialization)
}
dependencies {
...
implementation(libs.androidx.navigation3.ui)
implementation(libs.androidx.navigation3.runtime)
implementation(libs.androidx.lifecycle.viewmodel.navigation3)
implementation(libs.androidx.material3.adaptive.navigation3)
implementation(libs.kotlinx.serialization.core)
}
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-07-27 (世界標準時間)。"],[],[],null,["# Get started\n\nTo get started with Navigation 3, add the library to your project along with any\nsupporting libraries. Use the table below to decide which libraries to add.\n\nArtifacts\n---------\n\n| **Name** | **What it does** | **Artifact** |\n|-------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------|\n| Navigation 3 runtime library | Core Navigation 3 API. Includes `NavEntry, EntryProvider` and the associated DSL. | androidx.navigation3:navigation3-runtime |\n| Navigation 3 UI library | Provides classes to display content, including `NavDisplay` and `Scene`. | androidx.navigation3:navigation3-ui |\n| ViewModel Lifecycle for Navigation 3 | Allows ViewModels to be scoped to entries in the back stack. | androidx.lifecycle:lifecycle-viewmodel-navigation3 |\n| Material 3 adaptive layouts for Navigation 3 | Provides adaptive layouts (SceneStrategies, Scenes and metadata definitions) for use with NavDisplay. | androidx.compose.material3.adaptive:adaptive-navigation3 Available in [snapshot builds](https://androidx.dev/) |\n| [KotlinX Serialization](https://kotlinlang.org/docs/serialization.html) | Allows navigation keys to be serialized. | Plugin: org.jetbrains.kotlin.plugin.serialization Library: org.jetbrains.kotlinx:kotlinx-serialization-core |\n\nProject setup\n-------------\n\nTo add the Navigation 3 library to your existing project, add the following to\nyour `libs.versions.toml`: \n\n [versions]\n nav3Core = \"1.0.0-alpha01\"\n lifecycleViewmodelNav3 = \"1.0.0-alpha01\"\n kotlinSerialization = \"2.1.21\"\n kotlinxSerializationCore = \"1.8.1\"\n material3AdaptiveNav3 = \"1.0.0-SNAPSHOT\"\n\n [libraries]\n # Core Navigation 3 libraries\n androidx-navigation3-runtime = { module = \"androidx.navigation3:navigation3-runtime\", version.ref = \"nav3Core\" }\n androidx-navigation3-ui = { module = \"androidx.navigation3:navigation3-ui\", version.ref = \"nav3Core\" }\n\n # Optional add-on libraries\n androidx-lifecycle-viewmodel-navigation3 = { module = \"androidx.lifecycle:lifecycle-viewmodel-navigation3\", version.ref = \"lifecycleViewmodelNav3\" }\n kotlinx-serialization-core = { module = \"org.jetbrains.kotlinx:kotlinx-serialization-core\", version.ref = \"kotlinxSerializationCore\" }\n\n # Note: The Material3 adaptive layouts library for Navigation 3 is currently\n # only available in snapshot builds. Follow the instructions at androidx.dev to\n # add the snapshot builds repository to your project.\n androidx-material3-adaptive-navigation3 = { group = \"androidx.compose.material3.adaptive\", name = \"adaptive-navigation3\", version.ref = \"material3AdaptiveNav3\" }\n\n [plugins]\n # Optional plugins\n jetbrains-kotlin-serialization = { id = \"org.jetbrains.kotlin.plugin.serialization\", version.ref = \"kotlinSerialization\"}\n\nAlso, update your compile SDK to 36 or above: \n\n [versions]\n compileSdk = \"36\"\n\nAdd the following to your **app** build file `app/build.gradle.kts`: \n\n plugins {\n ...\n // Optional, provides the @Serialize annotation for autogeneration of Serializers.\n alias(libs.plugins.jetbrains.kotlin.serialization)\n }\n\n dependencies {\n ...\n implementation(libs.androidx.navigation3.ui)\n implementation(libs.androidx.navigation3.runtime)\n implementation(libs.androidx.lifecycle.viewmodel.navigation3)\n implementation(libs.androidx.material3.adaptive.navigation3)\n implementation(libs.kotlinx.serialization.core)\n }"]]