結束 Wear 上的全螢幕活動

使用者只要從左向右滑動,即可結束 Wear OS 活動。如果應用程式有水平捲動,使用者就會先前往內容邊緣,再從左向右滑動。此外,按下電源鍵也可讓使用者返回錶面。

滑動關閉手勢

使用者從左向右滑動,即可關閉目前的畫面。因此,建議您使用下列方法:

  • 垂直版面配置
  • 內容容器

我們也建議應用程式不要包含水平滑動手勢。

關閉活動

活動會自動支援滑動關閉。將活動從左向右滑動會關閉活動,應用程式則會向下瀏覽 返回堆疊

關閉片段

如要在片段中支援滑動關閉功能,您必須在 SwipeDismissFrameLayout 類別中納入包含片段的檢視畫面。決定是否要使用片段時,請將這一點納入考量。使用 SwipeDismissFrameLayout 類別,如以下範例所示:

Kotlin

class SwipeDismissFragment : Fragment() {
    private val callback = object : SwipeDismissFrameLayout.Callback() {
        override fun onSwipeStarted(layout: SwipeDismissFrameLayout) {
            // Optional
        }

        override fun onSwipeCanceled(layout: SwipeDismissFrameLayout) {
            // Optional
        }

        override fun onDismissed(layout: SwipeDismissFrameLayout) {
            // Code here for custom behavior, such as going up the
            // back stack and destroying the fragment but staying in the app.
        }
    }

    override fun onCreateView(
            inflater: LayoutInflater,
            container: ViewGroup?,
            savedInstanceState: Bundle?
    ): View =
            SwipeDismissFrameLayout(activity).apply {

                // If the fragment should fill the screen (optional), then in the layout file,
                // in the androidx.wear.widget.SwipeDismissFrameLayout element,
                // set the android:layout_width and android:layout_height attributes
                // to "match_parent".

                inflater.inflate(
                        R.layout.swipe_dismiss_frame_layout,
                        this,
                        false
                ).also { inflatedView ->
                    addView(inflatedView)
                }
                addCallback(callback)
            }
}

Java

public class SwipeDismissFragment extends Fragment {
  private final Callback callback =
    new Callback() {
      @Override
        public void onSwipeStart() {
          // Optional
        }

        @Override
        public void onSwipeCancelled() {
          // Optional
        }

        @Override
        public void onDismissed(SwipeDismissFrameLayout layout) {
          // Code here for custom behavior, such as going up the
          // back stack and destroying the fragment but staying in the app.
        }
      };

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    SwipeDismissFrameLayout swipeLayout = new SwipeDismissFrameLayout(getActivity());

    // If the fragment should fill the screen (optional), then in the layout file,
    // in the androidx.wear.widget.SwipeDismissFrameLayout element,
    // set the android:layout_width and android:layout_height attributes
    // to "match_parent".

    View inflatedView = inflater.inflate(R.layout.swipe_dismiss_frame_layout, swipeLayout, false);
    swipeLayout.addView(inflatedView);
    swipeLayout.addCallback(callback);

    return swipeLayout;
    }
}

注意:在活動中使用片段時,請使用 FragmentManager.add (而非 FragmentManager.replace) 來支援滑動關閉手勢。如此一來,即可確保先前的片段在滑開時顯示在頂端片段下方。

水平捲動的檢視畫面

在某些情況下,例如如果檢視畫麵包含支援平移的地圖,使用者介面就無法防止水平滑動。在這種情況下,有兩種選項:

  • 如果返回堆疊很短,使用者只要按下電源按鈕,即可關閉應用程式並返回錶面主畫面。
  • 如要讓使用者向下返回堆疊,可以將檢視畫面納入 SwipeDismissFrameLayout 物件中,該物件支援邊緣滑動。如果檢視畫面或子項從 canScrollHorizontally() 呼叫傳回 true,系統就會啟用邊緣滑動功能。邊緣滑動可讓使用者從螢幕最左邊 10% 的處 (而非檢視畫面中的任意位置) 滑動關閉檢視畫面。

以下範例說明如何將檢視畫面納入 SwipeDismissFrameLayout 物件中:

<androidx.wear.widget.SwipeDismissFrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/swipe_dismiss_root" >

    <TextView
        android:id="@+id/test_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="Swipe me to dismiss me." />
</androidx.wear.widget.SwipeDismissFrameLayout>

Kotlin

activity?.findViewById<SwipeDismissFrameLayout>(R.id.swipe_dismiss_root)?.apply {
    addCallback(object : SwipeDismissFrameLayout.Callback() {

        override fun onDismissed(layout: SwipeDismissFrameLayout) {
            layout.visibility = View.GONE
        }
    })
}

Java

SwipeDismissFrameLayout testLayout =
    (SwipeDismissFrameLayout) activity.findViewById(R.id.swipe_dismiss_root);
testLayout.addCallback(new SwipeDismissFrameLayout.Callback() {
    @Override
    public void onDismissed(SwipeDismissFrameLayout layout) {
        layout.setVisibility(View.GONE);
    }
  }
);

不建議:停用滑動關閉功能

一般來說,我們不建議停用滑動關閉,因為使用者會預期用滑動關閉任何畫面。在特殊情況下,您可以在 樣式資源中擴充預設主題,並將 android:windowSwipeToDismiss 屬性設為 false,如以下程式碼範例所示:

<resources>
  <style name="AppTheme" parent="@android:style/Theme.DeviceDefault">
    <item name="android:windowSwipeToDismiss">false</item>
  </style>
</resources>

接著,您可以告知使用者首次使用應用程式時,只要按下電源鍵即可退出應用程式。

使用電源按鈕關閉

按下實體電源鍵就會傳送電源鍵事件。因此,您無法使用電源按鈕做為返回按鈕,也無法用於一般導覽。

按下電源鍵後,電源按鈕會回到錶面主畫面。例外情況如下:

  • 如果使用者位於輸入法編輯器 (IME) 中 (例如手寫辨識畫面),按下按鈕會關閉 IME,並將使用者返回應用程式。
  • 如果使用者位於錶面,按下硬體按鈕即可開啟應用程式啟動器。

注意,按下電源鍵時,Activity 類別的 isFinishing() 方法不會傳回 true,且您無法攔截按鍵事件。

詳情請參閱「導覽」相關說明。