‫Welcome back.
‫In this video we'll talk about the fragments.
‫Life cycle.
‫The life cycle of a fragment in Android is a series of events that occur throughout its existence,
‫from the moment it's created, to the point when it's destroyed.
‫Understanding the fragment life cycle is crucial for managing the behavior and appearance of fragments
‫in your Android application.
‫Each fragment instance has its own life cycle.
‫When a user navigates and interacts with your app, your fragments transition through various states
‫in their life cycle as they are added, removed, and enter or exit the screen.
‫The fragment class includes callback methods that correspond to each of the changes in the fragments
‫life cycle.
‫Let's start with the on attach method.
‫This method is called when the fragment is attached to an activity.
‫It receives a reference to the hosting activity, allowing the fragment to interact with the Oncreate
‫method.
‫During this phase, the fragment is initialized.
‫You can perform one time set up tasks in this method, such as initializing variables and setting up
‫the fragments.
‫Initial state Oncreate view.
‫In this step, the fragment creates its user interface by inflating a layout file or constructing UI
‫elements programmatically.
‫The fragments, UI or user interface is prepared here on activity created.
‫This method is called after the hosting activities Oncreate method has finished executing.
‫It's a good place to perform any user interface related operations that require the activity to be fully
‫created.
‫You can see that the Oncreate method of the activity is called, and this is during the Oncreate method
‫of the activity.
‫On start, the fragment is visible to the user and it's about to become active.
‫You can start animations or other visual effects here or resume.
‫This is where the fragment becomes interactive.
‫Any code that needs to respond to user interactions should be placed here, such as starting background
‫tasks or other tasks on pose.
‫When the fragment about to be covered by another fragment or activity.
‫This method is called.
‫It's essential for saving the fragment state or stopping any ongoing tasks.
‫Onstop.
‫The fragment is no longer visible either because it's being replaced, the activity is paused or the
‫fragment is removed.
‫You should release any resources or tasks that are no longer needed.
‫Ondestroy view the fragments UI is destroyed in this step.
‫You can clean up any resources tied to the UI such as views and adapters.
‫Ondestroy.
‫It's called when the fragment is about to be destroyed completely, you should release any remaining
‫resources here.
‫On detach, the fragment is detached from the hosting activity.
‫At this point, it no longer has access to the activity and you should release any references or resources
‫tied to it.
‫It's important to note that the fragments can also go through various states based on their visibility,
‫such as being hidden or shown, and these lifecycle methods can be called multiple times as the fragments
‫visibility changes.
‫Understanding the fragment lifecycle allows you to manage the behavior of your fragments, effectively
‫ensuring that they respond correctly to the user interactions and that resources are allocated and released
‫appropriately during the fragments lifecycle.