According to the Android Developers, “A
Fragment represents a behavior or a portion of user interface in an
Activity. You can combine multiple fragments in a single activity to build a multi-pane UI and reuse a fragment in multiple activities.”
Following are some of the important point or lets say benefits of using Fragments:
- A fragment has its own layout and its own behaviour with its own life cycle callbacks.
- You can add or remove fragments in an activity while the activity is running.
- You can combine multiple fragments in a single activity to build a multi-plane UI.
- A fragment help developers reuse the components in multiple Activities, ViewPagers, and Navigation Drawers.
- Fragment life cycle is closely related to the life cycle of its host activity which means when the activity is paused, all the fragments available in the activity will also be stopped. In other words, we can say that they are managed by their own parent activity.
- A fragment can implement a behaviour that has no user interface component.
- Fragments were added to the Android API in Honeycomb version of Android which API version 11.
- They help in optimizing through different screens.
Fragments are of two types: Static fragments and Dynamic Fragments. Dynamic fragments can be further divided into Framework fragments and Support fragments.
Launching different activities separately for each app is not a good idea as it occupies more memory, space and can affect the performance of the app. It is advisable to use fragments whenever possible and simplify the app coding process.