Fragment
A Fragment
represents a reusable portion of your app’s UI. A fragment defines and manages its own layout, has its own lifecycle, and can handle its own input events. Fragments can’t live on their own. They must be hosted by an activity or another fragment. The fragment’s view hierarchy becomes part of, or attaches to, the host’s view hierarchy.
1
Modularity
Fragments introduce modularity and reusability into your activity’s UI by letting you divide the UI into discrete chunks. Activities are an ideal place to put global elements around your app’s user interface, such as a navigation drawer. Conversely, fragments are better suited to define and manage the UI of a single screen or portion of a screen.
The below image shows how two UI modules defined by fragments can be combined into one activity for a tablet design but separated for a handset design.2
Dividing your UI into fragments makes it easier to modify your activity’s appearance at runtime. While your activity is in the STARTED
lifecycle state or higher, fragments can be added, replaced, or removed. And you can keep a record of these changes in a back stack that is managed by the activity, so that the changes can be reversed. 3
Understanding Fragments
Here are the important things to understand about fragments:
- A
Fragment
is a combination of an XML layout file and a java class much like anActivity
; - Using the support library, fragments are supported back to all relevant Android versions;
- Fragments encapsulate views and logic so that it is easier to reuse within activities;
- Fragments are standalone components that can contain views, events and logic.
Within a fragment-oriented architecture, activities become navigational containers that are primarily responsible for navigation to other activities, presenting fragments and passing data. 4
Importance of Fragments
There are many use cases for fragments but the most common use cases include:
- Reusing View and Logic Components - Fragments enable re-use of parts of your screen including views and event logic over and over in different ways across many disparate activities. For example, using the same list across different data sources within an app.
- Tablet Support - Often within apps, the tablet version of an activity has a substantially different layout from the phone version which is different from the TV version. Fragments enable device-specific activities to reuse shared elements while also having differences.
- Screen Orientation - Often within apps, the portrait version of an activity has a substantially different layout from the landscape version. Fragments enable both orientations to reuse shared elements while also having differences. 5
How do we use Fragments?
Basically we have three ways to use fragments:
- Add fragment statically: add a fragment tag inside activity and set name to the fragment and give id and ready for use it: ```
2. **Add fragment dynamically:** In adding a fragment dynamically means we have to set the `FragmentManager` to begin transaction of fragment by using methods like `add()` or `replace()` and lastly we have to call commit method to initiate the transaction:
//dynamic way of adding fragment supportFragmentManager .beginTransaction() .replace(R.id.senderFragment, senderFragment) .commit() ```
Difference between add()
and replace()
can be illustrated by image:
add()
method keeps adding fragment on top of the previous fragment in theFragmentContainer
;replace()
method clears all the previous fragment from container and then add it inFragmentContainer
.
- Using Navigational component and set the fragment graph.
Links
[Introduction to Fragments | Android](https://www.geeksforgeeks.org/introduction-fragments-android/) |
Next questions
How to communicate with fragments?
Further Reading
Building dynamic user interfaces in Android with fragments - Tutorial
Advocating Against Android Fragments
7 Common Mistakes Easily Made with Android Fragment