In an Android – fragment is a part of an activity that allows a more modular activity design. It will not be wrong if we say that a fragment is a kind of sub-activity.
The following are important points about the fragment:
- A fragment has its own layout and behavior with its own callbacks in the life cycle.
- You can add or remove fragments to an activity while the activity is in progress.
- You can combine several fragments into a single activity to create a multi-panel UI.
- A fragment can be used in multiple activities.
- The life cycle of the fragment is closely related to the life cycle of its host activity, which means that when the activity is paused, all the fragments available in the activity will also stop.
- A fragment can implement behavior that does not have a user interface component.
- Fragments were added to the Android API in the Honeycomb version of Android than API version 11.
You can create fragments by extending the Fragment class and you can insert a fragment into the layout of your activity by declaring the fragment in the activity layout file, as an <fragment> element.
Before the introduction of the fragment, we had a limitation because we can only show a single activity on the screen at any given time. Therefore, we could not divide the device screen and control different parts separately. But with the introduction of the fragment, we obtained more flexibility and eliminated the limitation of having only one activity on the screen at a time. Now we can have only one activity, but each activity can comprise multiple fragments that will have their own layout, events and complete life cycle.
The following is a typical example of how two fragment-defined UI modules can be combined into an activity for a tablet design, but separated for handset design.
Read also: 4 Major Android Application Components.
When running on a tablet-sized device, the application can embed two fragments in Activity A.
However, on a handset-sized screen, there is not enough space for both fragments, so Activity A only includes the fragment for the article list, and when the user selects an article, it starts Activity B, which Include the second fragment to read the article.
Fragment of the life cycle
Android fragments have their own life cycle, similar to Android activity. This section summarizes the different stages of its life cycle.
Here is the list of methods that you can override in your fragment class:
- The onAttach () fragment instance is associated with an activity instance. The fragment and activity are not completely initialized. In general, you get a reference to the activity that the fragment uses for additional initialization work in this method.
- onCreate () The system calls this method when creating an onCreate () fragment. You must initialize the essential components of the fragment that you want to retain when the fragment stops or paused, and then resumes.
- onCreateView () The system calls this callback when it is time for the fragment to draw its user interface for the first time. To draw a UI for your fragment, you must return a View component of this method that is the root of the layout of your fragment. You can return null if the fragment does not provide a user interface.
- when the host activity is created, OnActivityCreated () is called after the onCreateView () method. The activity and the fragment instance have been created, as well as the hierarchy of views of the activity. At this point, the view can be accessed with the findViewById () method. example. In this method, you can create instances of objects that require a Context object
- onStart () The onStart () method is called once the fragment becomes visible.
- onResume () Fragment becomes active.
- The onPause () system calls this method the first indication that the user is leaving the fragment. This is usually where you should make changes that should continue beyond the current user session.
- onStop()Fragment going to be stopped by calling onStop()
- The onDestroyView () Fragment view will be destroyed after calling this method]
- onDestroy () onDestroy () called to do a final cleaning of the fragment state but it is not guaranteed that the Android platform will call it
How to use Fragments?
This involves several simple steps to create Fragments.
- First, decide how many fragments you want to use in an activity. For example, we want to use two fragments to handle the horizontal and vertical modes of the device.
- Then, based on the number of fragments, create classes that extend the Fragment class. The Fragment class has callback functions mentioned above. You can cancel any of the functions according to your requirements.
- Corresponding to each fragment, you must create design files in an XML file. These files will have a design for the defined fragments.
- Finally, modify the activity file to define the real logic of replacing fragments according to your requirements.
Types of fragments
Basically, the fragments are divided into three stages as shown below.
Fragments of a single frame: fragments of a single frame are used for handheld devices such as mobiles, here we can only show a fragment as a view.
List fragments: fragments that have a special list view are called list fragments
Fragment transaction: used with the fragment transaction. We can move a fragment to another fragment.
In this Article, Android training in Chandigarh will teach you basic Android programming and concepts related to Android application development.