 Hello, everyone. So today in this video, we are going to see fragment life cycle. Okay. I am Professor Dattatri Gangmal from Balchen Institute of Technology, from Computer Science and Engineering Department. So in this particular video, we are going to see fragment life cycle, what is fragment and how it is going to work. Learning outcomes. So students will be able to explain fragment in Android, and students will be also able to identify different states in fragment life cycle. We will see one by one. Now, first of all, we'll see what is fragment. A fragment represents a behavior or a portion of user interface in fragment activity. So we have already seen activity in activity, we can create a fragment. So fragment we can say in short, as a part of an activity. So once you create an activity inside activity, you can create a fragment. We will see with an example later. Now, how many fragments you can create in an activity? We can create multiple fragments in an activity. Here you can see that we can combine multiple fragments in a single activity. So our activity can contain n number of fragments. It depends upon the type of application you are creating. Based on the type of application you are creating, the number of fragments will vary. Fragment also has its own life cycle. Like we have seen activity life cycle. Similarly, fragment is also having a life cycle. We have already seen that importance of life cycle. If there is no life cycle, then we cannot pause or we cannot destroy a particular activity when it is running. Therefore, life cycle is an important part of Android application. So using this particular activity, you can add or remove while activity is in a running state. We will see in detail about it. Now, fragment must always be hosted in an activity. And the fragment's life cycle is directly affected by the host activity. As I am saying that fragment is a sub-child, child of an activity. So when you are creating an activity, inside activity, you can create a fragment as well. So you cannot create a fragment individually. There has to be activity, then only fragment will happen. For example, when activity is paused, all fragments in it and when activity is destroyed, so all are fragments. So when you try to pause an activity inside that fragment, inside it, a fragment will be created or it can be destroyed as well. And one important part to remember is while an activity is running, when an activity is running, it is in the life cycle state, resumed life cycle state. What is the meaning of it? You can manipulate each fragment independently. What is the meaning of manipulating? You can add a fragment, you can remove a fragment, you can alter the components of a fragment. This can be done when an activity is running. Now, when you add a fragment as a part of your activity layout, it lives in a view group. What is the meaning of view group? We have seen this view group while we have learned activity. So view group is a root of any activity is created. So when you create a fragment, the fragment is created or resides inside view group. So fragment's root cause is view group like similar to activity. I think hope this particular slide is clear to you all. You can pause anytime and watch it. You can take a note of it also. Now, this is important part of fragment. This is a fragment life cycle. As we have seen for activity, similarly for fragment life cycle. Fragment life cycle starts from here and here we have onattach, oncreate, oncreate view, onactivity, ondestroy. Likewise, we have this particular state. It has to start from top till it will go on moving to the bottom. Now, onpause, ondestroy. These all things will onstop, ondestroy. This is all activity life cycle. Now, when this particular state will happen? So these all particular methods, these are all methods inside fragment life cycle. We can have individual methods written when we try to create a fragment. So when you are creating a fragment, you can write all these methods and use it as and when required. So when this particular state transition will happen? See here, this is a state transition diagram which is happening, right? So fragment returns to the layout. Fragment is trying to return to its main layout. So when it returns to its main layout, it can again go to oncreate view. From oncreate view, it can again come back to onstart and it will be in the running state. When a fragment is under running state, there can be multiple fragments running inside it. So fragment will run only when an activity has started. So inside activity, this fragment will take place and once the content is over, the content view is over, then it will go to ondestroy and it will terminate. This is how fragment life cycle will take place. So we have seen that when you pause, when you pause, what will happen? How can you pause a particular fragment running? A fragment when it is running, you can pause it. Let's take an example of a game. In game, you can pause in between or you can take an example of video player. In video player also, when you are watching a movie or any kind of video, you can pause in between. Again, that will be from on pause. And when you come back, it will go to resume and it will play again. So this is how functioning of any fragment or any activity will take place. This is a very important diagram to understand the life cycle of a fragment. I hope you understood this particular slide. Moving on. Now this is a time to quiz. So you can pause a video over here and answer it. While we add a fragment in an activity, it gets added to which group? You can pause it and answer it. We have already seen this. What is the answer for it? The answer for this is view group. We have seen that view group, a particular thing here we have seen. It lives in a view group. Remember it is very important. Now embedding a fragment. How you can embed a fragment? This is how a fragment will look like when you try to embed it. There are two versions of it. Here you can see there are multiple fragments. Similarly, when you try to load the fragments inside a mobile phone, this is how it will look like inside a tablet, inside a mobile phone. This is how the view will change. Here I am trying to, for this particular event, this activity is generated and inside this activity there are two fragments. The upper part and the lower part. Similarly, if I try to load the same application inside a mobile phone or in an answered, this is how it will look like. For this particular event, activity is loaded and inside this activity there are two sections, section one and section two. I hope it is very clear from this particular diagram how activity, fragment is a part of an activity. We can have multiple fragments inside an activity. Next diagram you will understand clearly how fragment is part of an activity. Here you can see. Here this is fragment one and fragment two. These two fragments are residing on main activity. There is one main activity. On top of it, these two fragments are resided. Fragment one and fragment two. We can have many fragments. You can have them horizontally, vertically based on the need. You might have seen many applications likewise. There are many sections. There is a sound section, there is a volume section. These are all different fragments which will be loaded on your page. This is our two fragments. Below this, there is a third fragment. This is our fragment one, fragment two and fragment three. Likewise, you can have your own customized view based upon the need that customized view can be changed and can be brought into use. Fragment one, fragment two, fragment three. Likewise, you can have n number of fragments based upon the need. Here you can see the cut of the sections. If I want more fragments here, some horizontal fragments, I can reduce this particular fragment one, fragment two and introduce one more fragment over here. This is how views and fragments are created. This is one general example of how adding a fragment to an activity. Now, here you can see a linear layout. Inside linear layout, what I am doing here, here I am adding a fragment. Everything is the same. Like we have done for activity. Width, height, match pair and match pair. This will be the same. For a fragment, here this is a fragment section. In fragment section, what can I have? I can have a name of a fragment. Here name of a fragment, I am listing it inside. Article list fragment, it can be anything else. This is just a naming of a fragment. Then you have to give a unique ID to a fragment. The ID is list. I am adding a list view as a fragment. You might have seen when you click on a particular button, a list view comes and goes. This kind of activity, this kind of fragment will happen. List view comes and goes. That is the fragment. Again, we have a name. In ID, we can have a view. View is one type of fragment. Again, I am adding with particular height and width. Weight we have assigned instead of height. You can assign a weight also. Again, we have height, match pair. This is the general structure of fragment. These are some of the references which I have used. You can go through it and refer them. Thank you.