Android Fragment
A Fragment is a part of an Activity, enabling a more modular activity design. We can think of a Fragment as a sub-activity.
Here are the key points about Fragments:
* A Fragment has its own layout, its own behavior, and its own lifecycle 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-pane UI.
* Fragments can be used in multiple Activities.
* A Fragment's lifecycle is closely tied to its host Activity. This means that when the Activity is paused, all Fragments within it are stopped.
* Fragments can implement behavior without a user interface component.
* Fragments were added to the Android API in version 11.
You create a Fragment by extending the Fragment class. You can insert a Fragment into your Activity by declaring it in the activity's layout file using the `` element.
Before Fragments were introduced, we had a limitation because only a single Activity could be displayed on the screen at any given time. We couldn't split the device screen and control different parts independently. With the introduction of Fragments, we gained greater flexibility, and the restriction of having only a single Activity on the screen at one time was removed. Now we can have a single Activity, but each Activity is composed of multiple Fragments, each with its own layout, events, and complete 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, this application can embed two Fragments in Activity A. On a phone screen, due to limited space, Activity A only contains the Fragment with the article list. When the user clicks an article, it launches Activity B, which contains the second Fragment for reading the article.
* * *
## Fragment Lifecycle
Android Fragments have their own lifecycle, which is very similar to that of an Android Activity. 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:
* `onAttach()`: The Fragment instance is associated with an Activity instance. The Fragment and Activity are not fully initialized yet. Typically, you acquire a reference to the Activity in this method, which will be used in future initialization work of the Fragment.
* `onCreate()`: The system calls this method when creating the Fragment. You should initialize essential components of the Fragment that you want to retain when the Fragment is paused or stopped, so they can be restored.
* `onCreateView()`: The system calls this method when the Fragment is about to draw its user interface for the first time. To draw a UI for your Fragment, you must return a View component from this method that is the root of your Fragment's layout. If the Fragment does not provide a UI, simply return `null`.
* `onActivityCreated()`: This method is called after the host Activity's `onCreate()` method has returned. The Activity and Fragment instances and the Activity's view hierarchy are created. At this point, the view can be accessed using the `findViewById()` method. In this method, you can instantiate objects that require a Context object.
* `onStart()`: This method is called when the Fragment becomes visible.
* `onResume()`: This method is called when the Fragment becomes interactive.
* `onPause()`: The system calls this method when the first indication that the user is leaving the Fragment is given. Typically, this is where you should commit any changes that should persist beyond the current user session.
* `onStop()`: This method is called when the Fragment is about to be stopped.
* `onDestroyView()`: This method is called after the Fragment's view has been removed.
* `onDestroy()`: This method is used to clean up the Fragment's state. However, it is not guaranteed to be called by the Android platform.
* * *
## How to Use Fragments?
Here are the simple steps to create a Fragment:
* First, decide how many Fragments you need in your Activity. For example, we need to use two Fragments to handle both landscape and portrait modes of the device.
* Next, based on the number of Fragments, create classes that extend the `Fragment` class. The `Fragment` class includes the callback methods mentioned above. Override any methods as per your requirement.
* For each Fragment, you need to create a layout in an XML file. These files contain the defined layout for the Fragments.
* Finally, modify the Activity file as per the requirement to define the actual Fragment replacement logic.
* * *
## Fragment Types
Basic Fragments can be divided into three types as shown below:
- Single Frame Fragments are used on handheld devices like mobile phones. A single Fragment is displayed like a single video.
- A Fragment containing a special list view is called a List Fragment.
- Used with Fragment transactions. You can move from one Fragment to another.
YouTip