List Fragments - Part 2

Start course
Overview
Difficulty
Intermediate
Duration
1h 33m
Students
6
Ratings
3/5
starstarstarstar-borderstar-border
Description

This course explores fragment operators in Kotlin. You'll learn about the different types of fragments and how you can use them.

Learning Objectives

  • Understand the basics of fragments, including where and why we use them
  • Learn how to create them
  • Learn how to change fragments inside an activity with another fragment
  • Learn about fragment backtrack, list fragments, and dialog fragments

Intended Audience

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

Prerequisites

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

Transcript

Well, hi there everyone. So, in the previous video, we created a list fragment and an array adapter. We defined the array adapter to the list view. So, now we're going to continue where we left off. So, we'll go to Android Studio and I want to add a click listener to the list view. So, I'll just write in here, listView.setOnItemClickListener. Now, there are two set on item click listener methods here. So, I'll just select the method with curly braces next to it and press 'Enter'. So, we're going to write our necessary code in curly brackets. So, first of all I want to change the name of the i parameter from the parameters here. So, this I parameter is an integer type parameter and it gives us the position of each element that we'll click on in the list.

That's why I want to write position here instead of i. Also, if we look at the application again, when I click any element on the list, there's a new activity opening. So, what do I need to do? Create a second activity and I'll do that first. I will select the app folder from the project directory, click 'File', 'New Activity', Empty Activity. And I'm going to write 'SecondActivity' for the name of the second activity. And click 'Finish'. All right, so the second activity is now ready. So, I'll need to add a frame layout to this activity. The width and the height of the frame layout will be match_parent. Also, since our main layout is the constraint layout, we should define the constraints of this frame layout.

Right. So, I just want to pin it to the top, the right, the bottom and left. And then lastly, we'll give an id to this frame layout and the id can be 'frameLayout'. Now, to that frame layout, I want to add a new fragment. So now, I can close the main activity. I really don't have anything left to do in there anymore. I can also close the string XML folder and we'll create the second fragment now. So, you've already learned how to create a new fragment. This is how we create the fragment to move along a little faster. You can also create a separate design file and a separate content class if you want. So, I'm going to write SecondFragment for the name of the second fragment. Alright, so first of all I want to delete the unnecessary code here and only the fragment class and the onCreate view function in this class will remain. I'm just deleting all the other codes.

Now, the second fragment's also ready. Okay, so I want to add the second fragment inside second activity. So, I'll do this process just like in the main activity. First, I'll create an object from the fragment manager class. Then, I'll create an object from the fragment transaction class. Then, I'll create an object of the second fragment. So, after these I'm going to add the second fragment to the frame layout. And now, I can finish the process with a commit method. So, that way the second fragment was added to the second activity. So, when I open the second activity, the second fragment will open automatically. So, that means I must open the second activity inside Click Listener of the list view.

So, for this, I will open up the my first fragment class again and I can make this process just by using intent. So, I'll write, val intent = intent() and the intent class takes two parameters. The first one is the activity. Now, if I were supposed to run intent in an activity, the first parameter would be the name of the activity. But, since it's a fragment, we'll write activity. The second parameter, I'll write 'SecondActivity::class.java Then, I'll write startActivity(intent) You got that?

So, now when I click on any element in this list, the second activity and the second fragment will open. But, I've also got to send the position value of the clicked element to the fragment when it is open. So, as you know, all the elements have a position value in list view. Now, that position defines which element is selected from the list. So, the first element, the position value is zero. For the second element, it's one and so on. Third element's two. I will send the position value with intent after writing, intent.putExtra I will write keyword. So, the keyword can be positioned. Now, the value to be sent is the position value which is given to us as a parameter. So, let's get this value in the second activity. So, in the second activity, after writing, intent.getIntExtra() I will write the keyword and default value. The keyword will be position, default value can be 0. Of course, I will put this position value into an integer container. So, now when I click on any element in the list, both the second activity and therefore, the second fragment will open and the position of the element in the list will be sent to the second activity. The position value will also show the element that I selected.

Alright. So, now I've got to send that position value to the second fragment because I will make the process of showing the pictures in the second fragment. Now, you might not know it yet how to send data from activity to fragment. But, we're going to get to that. Here, I just want to mention briefly that we're going to be sending data between activities with intent but we're going to use bundle instead of intent between activity and fragment. It's really easy to use. It's actually very similar to using intent but I just want to show you how to use a bundle. Alright, so I'll send all of this data just by using the bundle method. So, first we'll need to create an object from the bundle class. I'll write, val bundle = Bundle()

Then I'll write, bundle.putInt() and this method gets two different parameters. The first one is the keyword. Keyword can be position again, and the second parameter is the value that's going to be sent. So, I'll write, position. And for the last one I'll write, secondFragment.arguments = bundle All right, so that's it for that. As you can see, it's really similar to data transfer with intent. Alright my friends, now there's only one thing left to do and that is to take the position value that we sent in the second fragment class and then show the image of the relevant city on the screen, according to this particular value. So, I want to take a short break here. We're going to do it in the next video. So, I want to see you there.

About the Author
Students
97
Courses
23
Learning Paths
1

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