Easy Tutorial
❮ Android Eclipse Android Studio Install ❯

Android Fragments

Fragments are a part of an activity that enable a more modular design of the activity. We can consider fragments as sub-activities.

Here are some important points about fragments:

Create fragments by inheriting the Fragment class. Before the introduction of fragments, we had a limitation where only a single activity could be displayed on the screen at a given time. We couldn't split the device screen and control different parts independently. With the introduction of fragments, we gained more flexibility and removed the limitation of having only one activity on the screen at a time. Now we can have a single activity composed of multiple fragments, each with its own layout, events, and full lifecycle.

Here is a typical example demonstrating how two UI modules defined by fragments can be combined in an activity designed for tablets and separated in an activity designed for handheld devices.

When running on a tablet-sized device, the application can embed two fragments in Activity A. On a phone screen, where there isn't enough space, Activity A only contains the fragment with the article list. When the user clicks an article, it launches Activity B containing the second fragment to read the article.


Fragment Lifecycle

Android fragments have their own lifecycle, similar to Android activities. Here is a brief introduction to the different stages of its lifecycle.

Here is a list of methods you can override in the Fragment class:


How to Use Fragments?

Here are the simple steps to create fragments:


Types of Fragments

Basic fragments can be divided into the following three types:

❮ Android Eclipse Android Studio Install ❯