ออกจากกิจกรรมแบบเต็มหน้าจอใน Wear

ลองใช้วิธีแบบ Compose
Jetpack Compose บน Wear OS เป็นชุดเครื่องมือ UI ที่แนะนำสำหรับ Wear OS

ผู้ใช้สามารถออกจากกิจกรรม Wear OS ได้โดยปัดจากซ้ายไปขวา หากแอปมีการเลื่อนในแนวนอน ผู้ใช้จะออกจากแอปโดยไปที่ขอบ ของเนื้อหาแล้วปัดจากซ้ายไปขวา การกดปุ่มเปิด/ปิดยังจะนำผู้ใช้กลับไปที่หน้าปัดด้วย

ท่าทางสัมผัสปัดเพื่อปิด

ผู้ใช้ปัดจากซ้ายไปขวาเพื่อปิดหน้าจอปัจจุบัน ดังนั้น เราขอแนะนำให้คุณใช้สิ่งต่อไปนี้

  • เลย์เอาต์แนวตั้ง
  • คอนเทนเนอร์เนื้อหา

นอกจากนี้ เรายังแนะนำว่าแอปของคุณไม่ควรมี ท่าทางสัมผัสการปัดในแนวนอน

ปิดกิจกรรม

กิจกรรมรองรับการปัดเพื่อปิดโดยอัตโนมัติ การปัดกิจกรรม จากซ้ายไปขวาจะส่งผลให้กิจกรรมปิดลง และแอป จะไปยัง สแต็กย้อนกลับ

ปิดส่วนย่อย

หากต้องการรองรับการปัดเพื่อปิดในส่วนย่อย คุณต้องใส่ View ที่มีส่วนย่อยไว้ในคลาส 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 เพื่อรองรับท่าทางสัมผัสปัดเพื่อปิด วิธีนี้จะช่วยให้มั่นใจได้ว่าส่วนย่อยก่อนหน้าจะแสดงผลใต้ส่วนย่อยด้านบนขณะที่ปัดออก

มุมมองที่เลื่อนได้ในแนวนอน

ในบางกรณี เช่น ในมุมมองที่มีแผนที่ซึ่งรองรับการเลื่อน ผู้ใช้จะปัดในแนวนอนได้เนื่องจากอินเทอร์เฟซผู้ใช้ไม่สามารถป้องกันการปัดในแนวนอนได้ ในสถานการณ์นี้ คุณมี 2 ตัวเลือกดังนี้

  • หาก Back Stack สั้น ผู้ใช้จะปิดแอปและกลับไปที่หน้าจอหลักของหน้าปัดได้โดยกดปุ่มเปิด/ปิด
  • หากต้องการให้ผู้ใช้ไปยังสแต็กย้อนกลับ คุณสามารถใส่ View ไว้ในออบเจ็กต์ SwipeDismissFrameLayout ซึ่งรองรับการปัดขอบ การปัดขอบจะเปิดใช้เมื่อ View หรือองค์ประกอบย่อยของ View แสดงผล true จากการเรียก canScrollHorizontally() การปัดขอบช่วยให้ผู้ใช้ ปิด View ได้โดยปัดจาก 10% ทางซ้ายสุดของหน้าจอ แทนที่จะปัดจากที่ใดก็ได้ใน View

ตัวอย่างต่อไปนี้แสดงวิธีใส่ View ไว้ในออบเจ็กต์ 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>

จากนั้นคุณสามารถแจ้งให้ผู้ใช้ทราบเมื่อใช้แอปเป็นครั้งแรก ว่าสามารถออกจากแอปได้โดยกดปุ่มเปิด/ปิด

การปิดด้วยปุ่มเปิด/ปิด

การกดปุ่มเปิด/ปิดจริงจะส่งเหตุการณ์ปุ่มเปิด/ปิด ดังนั้น คุณจึงไม่สามารถใช้ปุ่มเปิด/ปิดเป็นปุ่มย้อนกลับ หรือใช้สำหรับการนำทางโดยทั่วไป

เมื่อกดปุ่มเปิด/ปิด ระบบจะนำผู้ใช้กลับไปที่หน้าจอหลักของหน้าปัด โดยมีข้อยกเว้น 2 ประการ ดังนี้

  • หากผู้ใช้อยู่ในตัวแก้ไขวิธีการป้อนข้อมูล (IME) เช่น หน้าจอการจดจำลายมือ การกดปุ่ม จะปิด IME และนำผู้ใช้กลับไปที่แอป
  • หากผู้ใช้อยู่ที่หน้าปัด การกดปุ่มกด เปิดตัวเรียกใช้แอป

โปรดทราบ ว่าเมื่อกดปุ่มเปิด/ปิด เมธอด isFinishing() ของคลาส Activity จะไม่แสดงผล true และคุณจะสกัดกั้นเหตุการณ์ปุ่มไม่ได้

ดูข้อมูลเพิ่มเติมได้ที่ การนำทาง