Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Android Fragments


May 21, 2021 Android


Table of contents


Android Fragments

Fragments are part of the activity and are a more modular design of the activity. We can task fragments as a seed activity.

Here are some important points of knowledge about fragmentation -

  • Fragments have their own layout, their own behavior, and their own life cycle callbacks.
  • While the activity is running, you can add or remove fragments from the activity.
  • You can combine multiple fragments in a single activity to build a multi-column UI.
  • Fragments can be used in multiple activities.
  • The life cycle of a fragment is closely related to its host activity. This means that the activity is suspended and the fragments in all activities are stopped.
  • Fragmentation can implement behavior without user interface components.
  • Fragment is an Android API version 11 that is added to the Android API.

Create fragments by inheriting the Fragment class. You can insert fragments into your activity by using the element to declare fragments in the active layout file.

Before the introduction of fragmentation, we had a limitation because only a single activity could be displayed on the screen at a given point in time. W e can't split the device screen and control different parts independently. W ith the introduction of fragmentation, we gained more flexibility and allowed a point in time to be removed only with a single activity limit on the screen. Now we can have a single activity, but each activity is assembled by multiple fragments, each with its own layout, events, and complete lifecycle.

Here's a typical example of how to separate two fragmented UI modules in an activity designed for a tablet and in an activity designed for a handheld device.

Android Fragments

When running on a tablet-sized device, the application can embed two fragments in Activity A. On the phone device screen, because there is not enough space, Activity A contains only fragments of the article list, and when the user clicks on the article, Activity B, which contains the second fragment, is launched to read the article.


The life cycle of the fragment

Android Fragments has its own lifecycle, much like Android activity. Here's a brief introduction to the different phases of its lifecycle.

Android Fragments

Here's a list of ways you can override in class fragment:

  • onAttach(): Fragment instances are associated with active instances. F ragmentation and activity have not yet been fully initialized. Typically, you get a reference to the activity in the method and use it in the fragmentation's future initialization work.
  • onCreate(): When fragmentation is created, the system calls the method. Y ou need to initialize some of the necessary components for fragmentation. These components need to be preserved when fragments are paused and stopped for recovery.
  • OnCreateView(): The method is called when the fragment is about to draw its user interface for the first time. I n order to draw the fragmented UI, you need to return a View component from this method that represents the fragment root layout. If the fragment does not provide a user interface, return null directly.
  • onActivityCreated: When the host activity is created, the method is called after the onCreateView() method. A ctive and fragment instances are created at the view level with activities. A t this point, the view can be accessed through the findViewById() method. In this method, you can instantiate objects that require Context objects.
  • onStart(): The method is called when the fragment is visible.
  • onResume(): The method is called when fragments can interact.
  • onPause(): The method is called when the first indication is that the user is about to leave the fragment. Typically, here you need to commit any changes that will exceed the persistence of the user session.
  • onStop(): The fragment is called when it is about to be stopped.
  • OnDestroyView(): When the method is called, the fragments are about to be destroyed.
  • onDestroy(): This method is used to clean up the state of the fragment. But android platforms don't guarantee to be called.

How do I use fragments?

Here are some simple steps to create fragmentation.

  • Start by deciding how many fragments you need to use in the activity. For example, we need to use two fragments to handle both the horizontal and vertical modes of the device.
  • Next, create a class that inherits from the class Fragment based on the number of fragments. C lass Fragment contains the callback function mentioned above. Rewrite any method according to your needs.
  • For each fragment, you need to create a layout in the XML file. These files contain the layout of the defined fragments.
  • Finally, the actual fragmentation replacement logic is defined by modifying the active file based on requirements.

The fragment type

Basic fragments can be divided into three types as follows:

  • Single-frame fragmentation - Single-frame fragmentation is used by handheld devices such as mobile phones. A fragment appears like a video.
  • List Fragments - Fragments that contain a special list view are called list fragments.
  • Fragment transition - Used with fragment transactions. You can move from one fragment to another.