- 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:
- This fragment shows only one fragment as a view.
- Single frame fragments are used for hand-holding devices like smartphones,
- List Fragment:
- This fragment is normally used to represent in a special list view form.
- Fragment Transaction:
- 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 Methods | Fragment Life-Cycle |
Created | onAttach(), onCreate(), onCreateView(), onActivityCreated() | The fragment is added and its layout is inflated. |
Started | onStart() | The fragment is active and visible. |
Resumed | onResume() | The fragment is active and ready for user interaction. |
Paused | onPause() | The fragment is paused because the Activity is paused. |
Stopped | onStop() | 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():
- This method is called/used when a Fragment is first attached to a host Activity.
- onCreate():
- This method is used to create a new or fresh fragment.
- 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():
- This method is called when it’s time for the fragment to draw its user interface for the first time.
- This method returns null when the fragment does not have a UI.
- 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():
- This method is called after the onCreateView() method when the host activity is created and before the onViewStateRestored() method.
- This method is used to do final initialization, such as retrieving Views or restoring state.
- At this point, the view can be accessed with the findViewById() method.
- In this method, we can instantiate objects which require a Context object.
- onStart():
- This method is called once when the fragment becomes visible.
- onResume():
- In this method, the Fragment becomes active again.
- This method is called by the host Activity to resume a Fragment again that is visible to the user and actively running.
- onPause():
- onStop():
- The fragment is going to be stopped by using the onStop() method.
- onDestroyView():
- This method is called when the View previously created by onCreateView() has been detached from the Fragment.
- The fragment view will be destroyed after using this method.
- This call can occur if the host Activity has stopped, or the Activity has removed the Fragment.
- 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.
- The onDestroyView() method is called after onStop() and before onDestroy().
- onDestroy():
- This method is used or called to do the final clean-up of the fragment’s state.
- onAttach():
- The common methods of the Fragment Life cycle are-
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.
0 Comments