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.replace 대신 FragmentManager.add를 사용하여 스와이프하여 닫기 동작을 지원합니다. 이렇게 하면 이전 프래그먼트가 스와이프되는 동안 상단 프래그먼트 아래에서 렌더링됩니다.

가로 스크롤 가능 뷰

화면 이동을 지원하는 지도가 포함된 뷰에서와 같이 사용자 인터페이스가 가로 스와이프를 차단할 수 없는 경우도 있습니다. 이 시나리오에는 두 가지 옵션이 있습니다.

  • 백 스택이 짧으면 사용자는 앱을 닫고 전원 버튼을 눌러 시계 화면 홈 화면으로 돌아갈 수 있습니다.
  • 사용자가 백 스택으로 이동하도록 하려면 가장자리 스와이프를 지원하는 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를 반환하지 않으며 키 이벤트를 가로챌 수 없습니다.

자세한 내용은 탐색을 참고하세요.