![]() So, this inflate method is going to take three parameters. So, just right in here, return inflater.inflate. So after that, I need to change this return part, so this method will inflate our fragment. When I select this method, Android studio will take care of the rest. 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. The name of the method is the onCreateView method. Now, I'll need to overwrite a method in this class. 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'. Name is going to be FragmentExample, click 'Ok'. So, after that, I'll need to create a class for the fragment, right?Īre you a step ahead of me yet? I want to right click 'Main Activity Class', select New Kotlin Class. So now, we can see this fragment quite comfortably. Now also, I want to change the text size and text color of this textview. I will change the text of this TextView and I'm going to write 'This is a new fragment'. Okay, now I'm going to take this TextView and bring it to the center. And the background color can be dark red. Now, I want to change the background color. All right, so layout's created, I'll add a TextView to this layout. All right, so after that, I can give a name to the layout file and my name here is going to be fragment_new. 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'. So, for the fragment, we'll need to create a design file and a Kotlin class. Now, I want to show you how to create a fragment, okay? So, let's delete the next few component here. All right, so the design area will look like a phone screen natch. By clicking the 'eye icon' here, I'll choose the Show System UI option. So first, we got to open the activity_main.xml page. So as you can see, our project's created. So, our project is currently being created. So, the minimum SDK can be API 21.Īll right, so finally we're going to start creating our project just by clicking on the 'Finish' button. 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. The package name will be shown just like it is here. Of course, you can name it whatever you want. 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. I'll click on the file tab in the upper left corner and then, I'll click on the 'New' 'New Project'. All right, so we've talked about these algorithm milestones in previous videos and now I want to show these methods in Android Studio. You follow? So, after the onDetach() method, the fragment closes completely. Now, if you start again after the onDestroyView() method, it's going to begin from the onCreateView() method. If you start again a particular fragment, after the onStop() method, it will begin from the onStart() method. When the fragment closes, these methods start to work: onPause(), onStop(), onDestroyView(), onDestroy(), onDetach(). Now with the onResume() method, the fragment continues to work. After that, these three methods start to work, onViewCreated(), onStart(), onResume(). So, when a fragment first starts, these three methods start to work onAttach(), onCreate(), onCreateView(). We're going to talk about this particular life cycle right now. So, that means the fragment has a life cycle from the moment it was created to the moment it was closed. So, we've got to continue with the life cycles because that's what we got to do. And you know what? I think that's enough right now about fragments. Also, you can use one fragment in more than one activity. For example, you can create fragments as much as you want in one activity. And because of that, developers prefer using fragments. So, fragments are the more useful and more dynamic user interfaces. You can find fragments as small activity particles. ![]() Fragments are basically a user interface that works in the activity. So, fragments are presented to the developer after API level 11. 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. We're not going to go into too much detail about fragments. We're going to talk about the life cycle in fragments, all right? So, we will discuss fragments in detail in the following videos. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |