What are Android Fragment and their use?

Android Fragment is a part of an activity that represents multiple screens inside a single activity. There may be many fragments inside an activity. Android fragment is affected by an activity lifecycle as it resides under an activity. The fragment is a sub-activity. 

The fragment includes some important things. Such as; 

  • It contains its layout and it has self behaviour with its own lifecycle.

  • It is useful for multiple activities.

  • While running activity, it can add or remove any of the fragments. 

  • It includes different fragments in a single activity. 

  • Furthermore, a Fragment lifecycle is added to its host activity lifecycle. While the activity stops, all remaining fragments will stop working. 

To more information visit:android app development course.

Understanding Fragments

Fragments are a combination of an XML layout file and a Java class such as Activity. The fragments use a support library to support back all the Android versions. These are easy to reuse in activities as they include different types of views and logic. Furthermore, these are also known as standalone components. 

In this Android Fragment architecture, the activities become containers which are responsible for navigation to other activities and data transfers. A typical Android application uses only the activities that are organized like a tree structure. Here the root activities are initiated by a launcher. 

Importance of Fragments  

The fragments in Android have much importance. Fragments enable us to re-use the components of the screen such as views and logic in different ways across many activities. For instance, we can use the same data list across the data sources within an app. In addition to this, Fragments support activities of different devices to re-use shared elements. Such as, it supports the activities of tablet version which is different from mobile and television versions. 

It also supports screen orientation. It helps the screen sharing whether from portrait to landscape or vice-versa. 

Fragments are also known as content controllers. It includes most views, layouts, and logic. Based on the architecture we can divide the activities and fragments into two phases. Such as; activities for navigation and fragments for views and logics purposes.  

Android fragments under Android 3.0 support the dynamic and flexible UI designs especially on big screens like tablets. By dividing the layout of activity, we can change the activity appearance in runtime and can secure those changes. 

android.png

Fragment lifecycle

Similar to the activity lifecycle, the Android fragment has its own lifecycle. There are different stages in this lifecycle. These include different methods of a fragment. Such as;

onAttach(): Here it is attached to the activity. This method is like a reference to the activity where a fragment is used for initializing work further. 

onCreate(): While creating the Android fragment this method is called. This method is useful to initialize the essential elements of the fragment.

onCreateView(): Here it creates and returns the View component to build a User Interface for the fragment. If the fragment doesn’t provide a UI, then it returns null. 

onActivityCreated(): This is the next method after CreateView method. In this method of fragment activity, a View can be accessed by the findViewByld() method. 

onStart(): It is called once when the fragment is visible. 

onResume(): Here, the android fragment becomes active. 

onPause(): While the user leaves the Fragment, this method calls as a first indication. It is useful where any changes occur during the session. 

onStop(): Using this method, we can stop the ongoing Fragment. 

onDestroyView(): The Android fragment will destroy after calling this method. 

onDestroy(): This method is called to do the cleanup work finally. 

onDetach(): It mentions that fragment is no longer attached to the activity. 

Android fragment example source code

Creating a fragment for activity using XML. Here a layout file is created that adds a single android fragment to an activity. 


<RelativeLayout

    xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_height="match_parent"

    android:layout_width="match_parent">

    <fragment

        android:id="@+id/master_list_fragment"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:name="com.example.android.android_me.MasterListFragment">

    </fragment>

</RelativeLayout>


After creating the fragment we will check the activity by applying it. 

Later we can create Fragment class.

import android.app.Fragment;



public class MyFragment extends Fragment {


}

Like this there are various activities involved in creating these fragments. Each of these activities includes some class of commands to build a fragment class. 

Types of Android fragment

There are different types of Android fragments. Here are the key types of this fragment. 

Single frame fragments: These fragments are useful for devices like mobile. Here we can show only a single fragment for view. 

List fragments: This fragment has a list view.

Fragment transaction: This type of fragment is useful to move from one fragment to another. 

These are the main types of Android fragments that are useful for different activities under the Android environment. 

Use of Fragments

There are numerous steps involved in the creation of fragments and their usage. Here are the following. :-

  • At first, we need to decide the number of fragments to use within an activity. 

  • Next, we need to create a Fragment class. It involves fragment lifecycle instances. 

  • Relating to each fragment, we need to create layout files using XML files. 

  • Finally, we can modify the activity file to replace the fragments according to the requirement. 

Furthermore, there are some other uses of Fragments. In an Android app, each screen has a separate activity. This activity creates a challenge to pass information between the screens. As the Android intent doesn’t allow screen sharing, it is difficult to pass any information. Making each screen an individual Fragment, the passing of data can be avoided. 

With the increase in using Android Fragment, they are becoming the part of rich text UI design. And these are establishing a foundation for advanced UI metaphors. Fragments are the most useful features of an Android platform. 

Furthermore, we can combine different Fragments with a single activity to build a multi-panel UI and can reuse it for multiple activities. Moreover, Fragments have their own lifecycle, layouts, events and so on. 

Fragment container

This container is a special component that allows other components to specify the data requirements.A container doesn’t fetch data directly. It specifies the data for rendering purposes. The container is a parent view that holds the view structure of the fragment. FragmentContainerView is a specially designed layout for fragments. It extends Framelayout to handle Fragment transactions reliably. 

FragmentContainerView can be used to add a Fragment. For this, it uses android: name attribute. It is used as a container for Fragments. It cannot be used as a replacement for other ViewGroups outside of Fragment. Moreover, it has different uses that support the Android framework with more specifications.

Thus, the above writings explain about the Android Fragment and its use. A fragment is a sub-part of an activity that includes layout, views, and logic. It supports Android by sharing multiple screens within a single activity. Fragments have many uses in Android technology. Different types of Fragments serve different purposes. 

To develop a dynamic career in Android and its different platforms one can opt for android online training from various sources. This learning will not only enhance the skills but also opens the way to make a great successful career. 


Comments

Popular posts from this blog

Android App Project Package Structure (Android Studio)

ImageView and ImageButton in Android

Developing Android unit and instrumentation tests - Tutorial