Fragment는 앱 UI의 재사용 가능한 부분을 나타냅니다. 프래그먼트는 자체 레이아웃을 정의 및 관리하고 자체 수명 주기를 보유하며 자체 입력 이벤트를 처리할 수 있습니다. 프래그먼트는 단독으로 실행될 수 없습니다. 활동 또는 다른 프래그먼트에서 호스팅해야 합니다. 프래그먼트의 뷰 계층 구조는 호스트 뷰 계층 구조의 일부가 되거나 여기에 연결됩니다.
모듈성
프래그먼트는 UI를 개별 청크로 분할할 수 있도록 하여 활동의 UI에 모듈성과 재사용성을 도입합니다. 활동은 앱의 사용자 인터페이스(예: 탐색 창) 주위에 전역 요소를 배치하기에 적합합니다. 반대로 프래그먼트는 단일 화면이나 화면 일부의 UI를 정의하고 관리하는 데 더 적합합니다.
다양한 화면 크기에 반응하는 앱을 생각해 보세요. 큰 화면에서는 앱이 정적 탐색 창과 목록을 그리드 레이아웃으로 표시하도록 할 수 있습니다. 작은 화면에서는 앱이 하단 탐색 메뉴와 목록을 선형 레이아웃으로 표시하도록 할 수 있습니다.
활동에서 이러한 변형을 관리하는 것은 쉬운 일이 아닙니다. 콘텐츠에서 탐색 요소를 분리하면 이 프로세스를 더 쉽게 관리할 수 있습니다. 그러면 활동은 올바른 탐색 UI를 표시하고 프래그먼트는 적절한 레이아웃으로 목록을 표시합니다.
그림 1. 다른 화면 크기에서 동일한 화면의 두 가지 버전. 왼쪽에 있는 큰 화면에는 활동에서 제어하는 탐색 창과 프래그먼트에서 제어하는 그리드 목록이 포함되어 있습니다. 오른쪽에 있는 작은 화면에는 활동에서 제어하는 하단 탐색 메뉴와 프래그먼트에서 제어하는 선형 목록이 포함되어 있습니다.
UI를 프래그먼트로 나누면 런타임 시 활동의 모양을 더 쉽게 수정할 수 있습니다. 활동이 STARTED수명 주기 상태 이상에 있는 동안 프래그먼트를 추가하거나 교체, 삭제할 수 있습니다. 이러한 변경사항 기록을 활동에서 관리하는 백 스택에 보관할 수 있으므로 변경사항을 취소할 수 있습니다.
동일한 활동 내에서, 여러 활동에서 또는 다른 프래그먼트의 하위 요소로도 동일한 프래그먼트 클래스의 여러 인스턴스를 사용할 수 있습니다. 이 점을 고려하여 자체 UI를 관리하는 데 필요한 로직만 프래그먼트에 제공하세요. 한 프래그먼트에 의존하거나 다른 프래그먼트에서 프래그먼트를 조작하지 마세요.
이 페이지에 나와 있는 콘텐츠와 코드 샘플에는 콘텐츠 라이선스에서 설명하는 라이선스가 적용됩니다. 자바 및 OpenJDK는 Oracle 및 Oracle 계열사의 상표 또는 등록 상표입니다.
최종 업데이트: 2025-07-27(UTC)
[null,null,["최종 업데이트: 2025-07-27(UTC)"],[],[],null,["# Fragments\n\nA [`Fragment`](/reference/androidx/fragment/app/Fragment) represents a\nreusable portion of your app's UI. A fragment defines and manages its own\nlayout, has its own lifecycle, and can handle its own input events. Fragments\ncan't live on their own. They must be *hosted* by an activity or another\nfragment. The fragment's view hierarchy becomes part of, or *attaches to*,\nthe host's view hierarchy.\n| **Note:** Some [Android Jetpack](/jetpack/androidx/versions) libraries, such as [Navigation](/guide/navigation), [`BottomNavigationView`](/reference/com/google/android/material/bottomnavigation/BottomNavigationView), and [`ViewPager2`](/jetpack/androidx/releases/viewpager2), are designed to work with fragments.\n\nModularity\n----------\n\nFragments introduce modularity and reusability into your activity's UI by\nletting you divide the UI into discrete chunks. Activities are an ideal\nplace to put global elements around your app's user interface, such as a\nnavigation drawer. Conversely, fragments are better suited to define and\nmanage the UI of a single screen or portion of a screen.\n\nConsider an app that responds to various screen sizes. On larger screens, you\nmight want the app to display a static navigation drawer and a list in a grid\nlayout. On smaller screens, you might want the app to display a bottom\nnavigation bar and a list in a linear layout.\n\nManaging these variations in the activity is\nunwieldy. Separating the navigation elements from the content can make this\nprocess more manageable. The activity is then responsible for displaying\nthe correct navigation UI, while the fragment displays the list with the proper\nlayout.\n**Figure 1.** Two versions of the same screen on different screen sizes. On the left, a large screen contains a navigation drawer that is controlled by the activity and a grid list that is controlled by the fragment. On the right, a small screen contains a bottom navigation bar that is controlled by the activity and a linear list that is controlled by the fragment.\n\nDividing your UI into fragments makes it easier to modify your activity's\nappearance at runtime. While your activity is in the `STARTED`\n[lifecycle state](/guide/components/activities/activity-lifecycle) or\nhigher, fragments can be added, replaced, or removed. And you can keep a record\nof these changes in a back stack that is managed by the activity, so that\nthe changes can be reversed.\n\nYou can use multiple instances of the same fragment class within the\nsame activity, in multiple activities, or even as a child of another\nfragment. With this in mind, only provide a fragment with\nthe logic necessary to manage its own UI. Avoid depending on or\nmanipulating one fragment from another.\n\nNext steps\n----------\n\nFor more documentation and resources related to fragments, see the following.\n\n### Getting Started\n\n- [Create a fragment](/guide/fragments/create)\n\n### Further topics\n\n- [Fragment manager](/guide/fragments/fragmentmanager)\n- [Fragment transactions](/guide/fragments/transactions)\n- [Navigate between fragments using animations](/guide/fragments/animate)\n- [Fragment lifecycle](/guide/fragments/lifecycle)\n- [Saving state with fragments](/guide/fragments/saving-state)\n- [Communicate with fragments](/guide/fragments/communicate)\n- [Working with the AppBar](/guide/fragments/appbar)\n- [Display dialogs with DialogFragment](/guide/fragments/dialogs)\n- [Debug your fragments](/guide/fragments/debugging)\n- [Test your fragments](/guide/fragments/test)\n\n### Samples\n\n### Videos\n\n- [Single Activity: Why, when, and how (Android Dev Summit '18)](https://www.youtube.com/watch?v=2k8x8V77CrU)\n- [Fragments: Past, present, and future (Android Dev Summit '19)](https://www.youtube.com/watch?v=RS1IACnZLy4)"]]