• The fragment is a part of the Activity in which we can create and display one or multiple screens in one activity as per application requirements.
  • It is the modular section of the Android activity. Hence, it is sometimes called Sub-Activity.
  • A fragment encapsulates the functionality of an individual fragment so that it is easier to reuse within those activities and layouts.
  • We can also use the concept of fragments to create different layouts for landscape and portrait orientation for a smartphone. 
  • Types of Fragments:
    • There are three types of fragments:-
      • Single Fragment:
        1. This fragment shows only one fragment as a view.
        2. Single frame fragments are used for hand-holding devices like smartphones, 
      • List Fragment:
        1. This fragment is normally used to represent in a special list view form.
      • Fragment Transaction:
        1. This type of fragment is used to move information from one fragment to another fragment when required in an application.
  • Fragment Life-Cycle:
    • Android fragments have their own life cycle and are very much similar to an Android activity. 
    • Like an Activity, a Fragment has its own lifecycle but is affected by the activity life cycle because fragments are embedded/included in the activity.
    • An Activity that hosts a Fragment can send related information to that fragment, and receive information from that fragment. This is handled by the life cycle methods of both Activity and Fragments but is finally dominated by the Activity Life cycle.
    • A Fragment can’t communicate directly with another Fragment in an Activity. All Fragment-to-Fragment communication is done through the Activity that hosts them. One Fragment communicates with the Activity, and then the Activity communicates with the other Fragment.

Activity & Fragment Life Cycle Co-ordination:

States of Activity Fragment MethodsFragment Life-Cycle
Created
onAttach(), onCreate(), 
onCreateView(), 
onActivityCreated()
The fragment is added and its layout is inflated.
StartedonStart()The fragment is active and visible.
ResumedonResume()The fragment is active and ready for user interaction.
PausedonPause()The fragment is paused because the Activity is paused.
StoppedonStop()The fragment is stopped and no longer visible.
Destroyed
onDestroyView(), 
onDestroy(), 
onDetach()
The fragment is destroyed.

    • The common methods of the Fragment Life cycle are-
      • onAttach():
        1. This method is called/used when a Fragment is first attached to a host Activity.
      • onCreate():
        1. This method is used to create a new or fresh fragment.
        2. Here, we initialize the essential components of the fragment that we want to retain when the fragment is paused or stopped, and then resumed.
      • onCreateView():
        1. This method is called when it’s time for the fragment to draw its user interface for the first time.
        2. This method returns null when the fragment does not have a UI.
        3. This method returns the View component when the fragment does have a UI or draws a UI which is considered as the root of the fragment’s layout.
      • onActivityCreated():
        1. This method is called after the onCreateView() method when the host activity is created and before the onViewStateRestored() method.
        2. This method is used to do final initialization, such as retrieving Views or restoring state.
        3. At this point, the view can be accessed with the findViewById() method.
        4. In this method, we can instantiate objects which require a Context object.
      • onStart():
        1. This method is called once when the fragment becomes visible.
      • onResume():
        1. In this method, the Fragment becomes active again.
        2. This method is called by the host Activity to resume a Fragment again that is visible to the user and actively running.
      • onPause():
      • onStop():
        1. The fragment is going to be stopped by using the onStop() method.
      • onDestroyView():
        1. This method is called when the View previously created by onCreateView() has been detached from the Fragment.
        2. The fragment view will be destroyed after using this method.
        3. This call can occur if the host Activity has stopped, or the Activity has removed the Fragment.
        4. This method also performs some actions, such as logging a message, when the Fragment is no longer visible. The next time the Fragment needs to be displayed, a new View is created.
        5. The onDestroyView() method is called after onStop() and before onDestroy().
      • onDestroy():
        1. This method is used or called to do the final clean-up of the fragment’s state.

States of Fragment Life-Cycle:

A complete Fragment life cycle exists in three states when added with hosting Activity:-

  • Resumed(Active) State: 
    • In this state, the fragment is visible in the running activity.
    • In this state, there are two methods onCreate() and onCreateView() that perform the Active or Resumed state of the fragment life cycle.
    • Here, the onCreate() method Initializes essential components and variables of the Fragment.
    • This method is called when the Fragment is created.
    • Anything initialized using the onCreate() method is preserved even during the paused and the resumed state.
    • onCreateView() method is used to Inflate the XML UI layout for the Fragment. In other words, the system calls this method to draw the Fragment UI for the first time. As a result, the Fragment is visible in the host Activity. 
  • Paused State:[onPause()] 
    • In this state, another activity is in the foreground and has focus, but the activity in which this fragment lives is still visible.
    • Here, the foreground activity is partially transparent or doesn’t cover the entire screen.
    • The onPause() method saves any data and states that need to survive beyond the destruction of the Fragment. The system calls this method when:
      • The user navigates backward using the Back button.
      • The Fragment is replaced or removed, or another operation modifying the Fragment.
      • The host Activity is paused.
      • A paused Fragment remains alive but it will be destroyed if the Host Activity is destroyed.
      • When the user presses the Back button, the Fragment is returned from the back stack, and the lifecycle resumes by calling the onCreateView() method.
    • Stopped State: [onDestroyView()] 
      • In this state, the fragment is not visible.
      • Here, either the host activity has been stopped or the fragment has been removed from the activity but finally added to the back stack.
      • Here, a stopped fragment is still alive (all state and member information is retained by the system). However, it is no longer visible to the user and will be killed if the activity is killed.
      • Here, onDestroyView() method of this state is called when the View resources previously created by the onCreateView() method  have been detached from the Fragment.
      • The onDestroyView() method is called when the host Activity has stopped, or the Activity has removed the Fragment. This method is normally called after onStop() and before onDestroy() method.
    Categories: Android

    0 Comments

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    This site uses Akismet to reduce spam. Learn how your comment data is processed.