導航
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
試試 Compose 的方式
建議您使用 Wear OS 版 Jetpack Compose,這是 Wear OS 的推薦 UI 工具包。
為每個使用者歷程設計個別畫面後,您可能會有幾個垂直或單一畫面。接下來,您必須決定如何設計這些畫面,讓畫面能協同運作,以及如何實作導覽功能。
設計
請參考應用程式設計規範,讓應用程式階層保持淺層和線性。
首先,應用程式啟動器應開啟最常見的使用者歷程。設計每個使用者歷程時,請將最重要的內容放在最上方。對於直向容器,請使用底部連結至其他較不常見的使用者歷程和設定。
圖 1. 將最重要的內容放在垂直容器的頂端。
當使用者進入其中一個畫面時,請務必讓他們可以使用滑動關閉手勢,向下瀏覽返回堆疊。
實作導覽
實作導覽時,您有三個選項,如下所述:
- 僅限活動 (建議做法)
- 活動和片段
- Jetpack Navigation
僅限活動
由於直向畫面通常只有一層深,因此您可以使用活動實作所有畫面,而不需要使用片段。
我們強烈建議採用這種做法。這可簡化程式碼,且活動會自動支援滑動關閉功能。這也能簡化微光模式的實作方式。
注意:如果您未使用片段,請讓活動繼承自 ComponentActivity
。其他活動類型使用的是行動裝置專用的 UI 元素,Wear OS 不需要這類元素。
活動和片段
您可以在 Wear OS 應用程式中使用片段與活動。不過,我們不建議這麼做,因為使用片段建立淺層和平面架構並無明顯優勢。
注意:如果您使用片段,請讓片段繼承 FragmentActivity
。其他活動類型使用的是行動裝置專用的 UI 元素,Wear OS 不需要這類元素。
在 Wear OS 應用程式中使用片段時,可能會遇到下列難題:
-
您必須自行實作滑動關閉功能。否則,使用者滑動時會退出整個應用程式。
-
如果您使用
AmbientMode
,必須進行自訂才能正常運作。AmbientMode
會在活動上設定,因此在實作片段時,您必須將此納入考量。
如要支援使用片段的滑動關閉功能,您必須在 SwipeDismissFrameLayout
類別中包裝含有片段的檢視畫面。詳情請參閱「滑動關閉手勢」。這樣一來,使用者就能享有一致的應用程式體驗。
注意:使用片段時,請使用 FragmentManager.add
而非 FragmentManager.replace
來支援滑動關閉手勢。這有助於確保先前的片段在滑動時,會在頂層片段下方算繪。
Jetpack Navigation
Jetpack Navigation 可在 Wear OS 上運作,但它與片段具有相同的缺點。這會增加開發工作量,而且由於 Wear OS 應用程式的階層通常較淺且線性,因此不會提供太多優勢。最佳做法是只使用活動。
如要充分運用 Jetpack Navigation,請執行下列操作:
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-26 (世界標準時間)。
[[["容易理解","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-26 (世界標準時間)。"],[],[],null,["# Navigation\n\nTry the Compose way \nJetpack Compose on Wear OS is the recommended UI toolkit for Wear OS. \n[Handle navigation using Compose on Wear OS →](/training/wearables/compose/navigation) \n\n\nAfter designing individual screens for each user journey, you might have a few vertical or single\nscreens.\nNext, you have to decide how to design these screens to work together and how to implement the\nnavigation.\n\nDesign\n------\n\n\nKeep your app's hierarchy shallow and linear, as called out in the [app design guidelines](/training/wearables/design/apps).\n\n\nTo start, your app's launcher should open the most common user journey. Design each user journey\nwith the most important content at the top. For vertical containers, use the bottom to link to\nother, less-common user journeys and the settings.\n\n\n**Figure 1.** Keep the most important content at the top of vertical containers.\n\nWhen users enter one of your screens, make sure they can use the swipe-to-dismiss gesture\nto navigate down the [back stack](/guide/components/tasks-and-back-stack).\n\nImplement the navigation\n------------------------\n\n\nWhen you implement your navigation, you have three options, described in the following sections:\n\n- Activities only, which is the recommended approach\n- Activities and fragments\n- Jetpack Navigation\n\n### Activities only\n\n\nSince vertical screens are typically one level deep, you can implement all your screens\nusing activities and without using fragments.\n\n\nWe strongly recommend this approach. It simplifies your code, and\nactivities automatically support\n[swipe-to-dismiss](/training/wearables/apps/exit#swipe-to-dismiss). This also makes\nimplementing [ambient mode](/training/wearables/apps/always-on) simpler.\n\n\n**Note:** Make your activities inherit from a\n[`ComponentActivity`](/reference/androidx/activity/ComponentActivity) if\nyou are not using fragments. The other activity types use mobile-specific UI elements you don't\nneed for Wear OS.\n\n### Activities and fragments\n\n\nYou can use [fragments](/guide/fragments) with activities in your Wear OS app. However,\nwe don't recommend this, as there isn't a clear advantage to using fragments to create a shallow and flat\narchitecture.\n\n\n**Note:** If you are using fragments, make them inherit from\n[`FragmentActivity`](/reference/androidx/fragment/app/FragmentActivity). The\nother activity types use mobile-specific UI elements you don't need for Wear OS.\n\n\nSome of the difficulties with using fragments in your Wear OS app include the following:\n\n- You must implement the swipe-to-dismiss yourself. Otherwise, when the user performs a swipe, they exit the entire app.\n- If you are using [`AmbientMode`](/reference/androidx/wear/ambient/AmbientMode), you must customize it to work properly. `AmbientMode` is set on the activity, so you have to take that into account when implementing fragments.\n\n\nTo support swipe-to-dismiss with fragments, you must wrap the fragment-containing\nview in the\n[`SwipeDismissFrameLayout`](/reference/androidx/wear/widget/SwipeDismissFrameLayout)\nclass. See\n[The swipe-to-dismiss gesture](/training/wearables/apps/exit#swipe-to-dismiss)\nfor more information. Doing this provides users a consistent experience with your app.\n\n\n**Note:** When using fragments, use\n[`FragmentManager.add`](/reference/androidx/fragment/app/FragmentTransaction#add(int,%20java.lang.Class\u003c?%20extends%20androidx.fragment.app.Fragment\u003e,%20android.os.Bundle))\nrather than\n[`FragmentManager.replace`](/reference/androidx/fragment/app/FragmentTransaction#replace(int,%20java.lang.Class\u003c?%20extends%20androidx.fragment.app.Fragment\u003e,%20android.os.Bundle))\nto support the swipe-to-dismiss gesture.\nThis helps ensure that your previous fragment renders under the top fragment while it is being\nswiped away.\n\n### Jetpack Navigation\n\n\nJetpack Navigation can work on Wear OS, but it has the same drawbacks as fragments. It adds\ndevelopment work and, because a Wear OS app's hierarchy is generally shallow and linear, it doesn't\noffer many advantages. An activity-only approach is best.\n\n\nTo fully leverage Jetpack Navigation, do the following:\n\n- Make sure every fragment uses a [`SwipeDismissFrameLayout`](/reference/kotlin/androidx/wear/widget/SwipeDismissFrameLayout) as its root, and manually use the dismiss action to go back in the navigation graph.\n- Implement a custom [`FragmentNavigator`](/reference/androidx/navigation/fragment/FragmentNavigator) that renders fragments on top of each other."]]