1. Home
  2. Training Library
  3. Programming
  4. Programming Courses
  5. Intent & Lifecycles in Android App Development

The Fragment Lifecycle



The course is part of this learning path

Start course
2h 15m

This course focuses on lifecycles and intent in Android and then moves on to look at services, receivers, and Android app binding.

Intended Audience

This course is intended for anyone who wants to learn how to start building their own apps on Android.


To get the most out of this course, you should have some basic knowledge of the fundamentals of Android.


Course GitHub repo: https://github.com/OakAcademy/android-app-development-with-kotlin/tree/main/Section%207%20-%20Intent%20and%20Lifecycle/Intent 



Hello everyone. We're going to talk about the life cycle in fragments, all right? So, we will discuss fragments in detail in the following videos. We're not going to go into too much detail about fragments. In this particular video, we just want to talk about life cycles and fragment first, okay? So just a little bit of information about fragments. So, fragments are presented to the developer after API level 11. Fragments are basically a user interface that works in the activity. You can find fragments as small activity particles.

So, fragments are the more useful and more dynamic user interfaces. And because of that, developers prefer using fragments. For example, you can create fragments as much as you want in one activity. Also, you can use one fragment in more than one activity. And you know what? I think that's enough right now about fragments. It gives you a little bit of a taste. So, we've got to continue with the life cycles because that's what we got to do. So, fragments have their own life cycle. So, that means the fragment has a life cycle from the moment it was created to the moment it was closed.

We're going to talk about this particular life cycle right now. So, when a fragment first starts, these three methods start to work onAttach(), onCreate(), onCreateView(). After that, these three methods start to work, onViewCreated(), onStart(), onResume(). Now with the onResume() method, the fragment continues to work. When the fragment closes, these methods start to work: onPause(), onStop(), onDestroyView(), onDestroy(), onDetach(). Also, there are these milestones. If you start again a particular fragment, after the onStop() method, it will begin from the onStart() method. Now, if you start again after the onDestroyView() method, it's going to begin from the onCreateView() method. You follow? So, after the onDetach() method, the fragment closes completely. All right, so we've talked about these algorithm milestones in previous videos and now I want to show these methods in Android Studio. So first, I'm creating a new project. I'll click on the file tab in the upper left corner and then, I'll click on the 'New' 'New Project'.

So, after selecting Empty Activity from the page that opens, I'm going to click the 'Next' button and I'll just pick a name out of a hat and I'm going to name this project Fragment Lifecycle. Of course, you can name it whatever you want. The package name will be shown just like it is here. Now I do not change the location where the project is going to be saved but if you want to, just keep track of it, change the location, and you can do that just by clicking here. So, the minimum SDK can be API 21.

All right, so finally we're going to start creating our project just by clicking on the 'Finish' button. So, our project is currently being created. It just takes a moment. Okay, my friends. So as you can see, our project's created. So first, we got to open the activity_main.xml page. By clicking the 'eye icon' here, I'll choose the Show System UI option. All right, so the design area will look like a phone screen natch. So, let's delete the next few component here. I've completely emptied my design area. Now, I want to show you how to create a fragment, okay?

So, for the fragment, we'll need to create a design file and a Kotlin class. And then, we needed to find this fragment class in the main activity or in which activity we want to show the fragment, you know, it, right? So, therefore, first of all, I'm just going to create a design file and I will go to Layout Folder, right click the 'Layout Folder', and select 'New' 'Layout Resource File'. All right, so after that, I can give a name to the layout file and my name here is going to be fragment_new. I'm going to click 'Ok'. All right, so layout's created, I'll add a TextView to this layout. Now, I want to change the background color. And the background color can be dark red. Okay, now I'm going to take this TextView and bring it to the center. I will change the text of this TextView and I'm going to write 'This is a new fragment'. Now also, I want to change the text size and text color of this textview. The size can be 24sp. Text color, it can be white. So now, we can see this fragment quite comfortably. So, after that, I'll need to create a class for the fragment, right?

Are you a step ahead of me yet? I want to right click 'Main Activity Class', select New Kotlin Class. Now, we're going to need to name it. Name is going to be FragmentExample, click 'Ok'. So after that, this class needs to extend the fragment class, right? So after the colon, I'm going to write fragment and press 'Enter'. Now, I'll need to overwrite a method in this class. The name of the method is the onCreateView method. We will define the design that we made for the fragment in the fragment class in this method, all right? So, I'll write onCreateView. When I select this method, Android studio will take care of the rest.

So after that, I need to change this return part, so this method will inflate our fragment. So, just right in here, return inflater.inflate. So, this inflate method is going to take three parameters. First one is fragment layout name, so I'm going to write in here R.Layout. I will write here the layout name and the name is fragment_new. The second parameter here is the container, that object of the view group. Now, this is a constant. And the third one is false. All right, so now the fragment class is ready.

Now, I just need to add this fragment to the main activity. So, we'll open the activity_main.xml file. You see, there is a component in which we can view the fragment design that we have created. The name of the component is FragmentContainerView. You can always find this component in the Containers section of the palette. So, I'm going to drag the FragmentContainerView from the palette, drop it in here and after that, a window is going to open up, and I will select here FragmentExample as my class and click 'Okay'. Now the fragment is added to the main activity, so, I will change the sizes of this component. Layout height, layout width can be 300dp. Also I'm going going to center this component. Now, the fragment is ready. So, let's run it and see.

All right, so there you go. You see how the fragments ready and of course, this is just the simplest of fragments. Of course, we're not going to use it like this. Just put a fragment in the activity and leave it like this, okay? Fragments aren't usually like this, I just wanted to show you how to create a simple fragment because in this video, remember we just wanted to see the fragment lifecycle, not how to use fragments in your project. Anyway, so after all that, we can now see which methods work in the fragment lifecycle. Where will I write the life cycle methods?

But of course, in the fragment class. So, I'm going to create another life cycle method in the fragment example class. And I'll first call the onAttach() method and we can add some other methods together. Second, I will call the onCreate() method. Third is the onCreateView() method. We already created this. The fourth, onViewCreated(). The fifth, onStart(). The sixth, onResume(). After the onResume() function, the fragment will run. So, the seventh onPause(). The eighth, onStop(). The ninth, onDestroyView(). Tenth, onDestroy(). And the last function is onDetach(). All right, so I added all these methods in the fragment life cycle.

Now after that, I'll just need to add log records in these methods, because I do want to see which method works when and where. So, Log.i. And it takes two parameters. First one is the title. I'll write fragment for the title. Second one is the message which method works, I want to write the name the method and the message. So, I'll copy this log record line and paste it into the other methods. So, after that I will change the messages. So, for the onCreate method, I will write in the message, onCreate. For the onCreateView method, I'll write onCreateView. The OnViewCreated method, OnViewCreated. And of course, it will be the same for the rest. onStart method, I'll just write onStart. The onResume method, I can write onResume. OnPause method gets onPause. And for the onStop method, I'll write onStop. For the onDestroyView method, I'll write onDestroyView. For the onDestroy method, I will just write onDestroy. And for the onDetach method, I'll write onDetach.

So, now I can track records here. I'm not going to select Debug anymore. I'll just select 'Info' because the letter I is shortened for to stand for info. So, there you go. Also, I want to be able to filter records with a fragment keyword. So, I'll write fragment into the search bar. See, now I can run the application. And my friends, what you can see here when the application first opens, onDetach, onCreate, onCreateView, onViewCreated, onStart and onResume methods all work. So, until this fragments created, these six methods work and we can see this on the algorithm. So, if I close the application, onPause, onStop, onDestroyView, onDestroy and onDetach methods will work.

Now, if I open the application again, these methods of course will work again. I say of course, but you might not know. Now, this is the caveat here. It doesn't mean that other methods do not work in the main activity. They will also continue to work. When we open the application, the main activity is created, and then after that the fragment is created. So, let's have a look in practice, all right. So, let's just open up an old project, and I will copy the log records that we wrote in the main activity and paste them in the main activity in my new project. You see what we're doing? So, that way we can follow the life cycles of both the activity and the fragment at the same time. Pretty cool.

Okay. So now, we can see the main activity log records in the logcat screen. But here I got to select the 'Debug' log records because we use the Log.d in the main activity. And also I'm going to delete the fragment keyword in the search bar. And here, I'll just write message. So, let's run the application again. And there you can see, when I run the application, activity lifecycle methods work. And now, if I change the Debug to Info, and also I got to write the message keyword instead of fragment. Okay, see the fragment lifecycle methods also work at the same time. All right. So, you can see these methods in detail for yourself. You just need to play around a little bit. You can change these log records and see all of the message together. That's pretty much up to you. But in any event, that's enough for this video, don't you think? That is pretty much all I want to tell you about the lifecycle of fragments. So, I'll see you in the next video.


About the Author
Learning Paths

Mehmet graduated from the Electrical & Electronics Engineering Department of the Turkish Military Academy in 2014 and then worked in the Turkish Armed Forces for four years. Later, he decided to become an instructor to share what he knew about programming with his students. He’s currently an Android instructor, is married, and has a daughter.

Covered Topics