Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Fragment thể hiện phần giao diện người dùng có thể tái sử dụng của ứng dụng. Mỗi mảnh xác định và quản lý bố cục riêng, có vòng đời riêng và có thể xử lý các sự kiện đầu vào riêng. Các mảnh không thể tồn tại độc lập mà phải được một hoạt động hoặc mảnh khác lưu trữ. Hệ phân cấp khung hiển thị của mảnh trở thành một phần hoặc đính kèm với hệ phân cấp khung hiển thị của máy chủ lưu trữ.
Mô-đun
Các mảnh cho phép bạn chia giao diện người dùng của hoạt động thành các phần riêng biệt, từ đó giao diện người dùng được phân chia theo mô-đun và có thể tái sử dụng. Các hoạt động là vị trí lý tưởng để đặt các phần tử chung xung quanh giao diện người dùng của ứng dụng, chẳng hạn như một ngăn điều hướng. Ngược lại, các đoạn phù hợp hơn để xác định và
quản lý giao diện người dùng của một màn hình hoặc một phần màn hình.
Hãy xem xét một ứng dụng tương thích với nhiều kích thước màn hình. Trên màn hình lớn, bạn có thể muốn ứng dụng hiển thị một ngăn điều hướng tĩnh và một danh sách trong bố cục lưới. Trên màn hình nhỏ hơn, bạn có thể muốn ứng dụng hiển thị một thanh điều hướng ở dưới cùng và một danh sách trong bố cục tuyến tính.
Bạn sẽ khó quản lý các biến thể này trong hoạt động. Việc tách riêng các phần tử điều hướng khỏi nội dung có thể làm cho quá trình này dễ quản lý hơn. Khi đó, hoạt động sẽ chịu trách nhiệm hiển thị giao diện người dùng điều hướng chính xác, còn mảnh hiển thị danh sách với bố cục phù hợp.
Hình 1. Hai phiên bản của cùng một màn hình trên nhiều
kích thước màn hình. Ở bên trái, màn hình lớn chứa ngăn điều hướng
do hoạt động kiểm soát và danh sách dạng lưới do mảnh
kiểm soát. Ở bên phải, màn hình nhỏ chứa thanh điều hướng phía dưới cùng
do hoạt động kiểm soát và danh sách tuyến tính
do mảnh kiểm soát.
Việc chia giao diện người dùng thành các mảnh sẽ giúp bạn dễ dàng sửa đổi giao diện
hoạt động của mình trong thời gian chạy. Khi hoạt động của bạn ở trạng thái vòng đờiSTARTED trở lên, bạn có thể thêm, thay thế hoặc loại bỏ các mảnh. Bạn có thể lưu giữ bản ghi về những thay đổi này trong ngăn xếp lui do hoạt động quản lý để có thể đảo ngược các thay đổi.
Bạn có thể sử dụng nhiều phiên bản của cùng một lớp mảnh trong cùng một hoạt động, trong nhiều hoạt động hoặc thậm chí dùng làm phần tử con của một mảnh khác. Vì vậy, hãy chỉ cung cấp cho mảnh logic cần thiết để quản lý giao diện người dùng của mảnh đó. Tránh phụ thuộc vào hoặc thao tác với một mảnh từ mảnh khác.
Bước tiếp theo
Để biết thêm tài liệu và tài nguyên liên quan đến mảnh, hãy xem các phần sau.
Nội dung và mã mẫu trên trang này phải tuân thủ các giấy phép như mô tả trong phần Giấy phép nội dung. Java và OpenJDK là nhãn hiệu hoặc nhãn hiệu đã đăng ký của Oracle và/hoặc đơn vị liên kết của Oracle.
Cập nhật lần gần đây nhất: 2025-07-27 UTC.
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 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)"]]