View video tutorial

ANDROID Fragment

ANDROID

Fragments in Android represent a reusable and modular part of an app's user interface (UI).

Android Fragment


➔ An Android fragment is a reusable component and can be used in different parts of an application as part of the user interface (UI).

➔ A fragment can be described as a "sub-activity" and handles its own layout, lifecycle, and input events, but it cannot exist independently, it must be hosted by an Android activity or another fragment.

➔ Instead of creating a new activity for each screen change, fragments were introduced to handle screen variations by allowing developers to dynamically swap or change UI elements.

Advantages of Fragments


Modularity: A complex UI can be divided into multiple parts, making it easier to manage and update specific parts of the screen.

Reusability: A single fragment can be used in multiple activities, and can even be placed inside other fragments.

Dynamic UI: Fragments allow us to add, remove, or replace UI components at runtime without reloading the entire activity.

Adaptability: Fragments are essential and very useful for creating responsive designs that look different on phones and tablets (e.g., a single-column list on a phone and a multi-column split view on a tablet).

Fragments Basic Components


FragmentManager: The class that handles fragment operations, such as adding or removing fragments, from the activity that hosts them.

FragmentTransaction: Represents a set of operations (add, remove, replace) performed together, so that users can undo changes with the "Back" button if necessary.

FragmentContainerView: This is a highly recommended layout container for holding fragments in an activity's XML.

Fragment Lifecycle


The Android fragment lifecycle consists of multiple states and callback methods, which manage the existence and various phases of a fragment, from its creation to its destruction.

The lifecycle of a fragment is closely tied to its host activity, meaning that the lifecycle of a fragment depends entirely on the lifecycle of the activity, but the fragment includes its own unique callbacks.

Core Lifecycle Callbacks of Fragment

When a fragment goes through its various states from creation to destruction, the system invokes the following callbacks


onAttach(): This is called first when the fragment is attached to its host activity or parent fragment.

onCreate(): This callback is used to initialize the essential components of the fragment, which should be maintained even when the fragment is paused or stopped.

onCreateView(): It is called to create the fragment's user interface and must return a view (the root of the layout) or null for a fragment without a UI.

onViewCreated(): This is invoked immediately after onCreateView() returns a non-null view. This is the recommended place to set up view properties, for example setting up adapters for RecyclerView.

onViewStateRestored(): This is called when all saved state in the fragment's view hierarchy has been restored.

onStart(): This callback is run when the fragment becomes visible to the user.

onResume(): This is invoked when the fragment is visible, all animations are complete, and it is ready for user interaction.

onPause(): The first indication that the user is leaving the fragment, so commit any changes here that should persist beyond the current session.

onStop(): This function is called when the fragment is no longer visible, because it has completely disappeared from the user's view.

onDestroyView(): This is invoked when the view hierarchy associated with the fragment is being removed and any references to the view should be cleaned up here.

onDestroy(): This callback is called to finally clear the fragment's state.

onDetach(): The final callback, which is called when the fragment detaches from its host.

Figure: Fragment Life-Cycle Methods

Figure: Fragment Life-Cycle Methods