結束 Wear 上的全螢幕活動

試試 Compose
建議您使用 Wear OS 專用的 Jetpack Compose UI 工具包。

使用者可以從左向右滑動,結束 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,且您無法攔截按鍵事件。

詳情請參閱「導覽」。