General Information about Fragments

General Information about Fragments
1h 33m

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.


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


Alright. So, hello everyone. We're going to talk about how to create fragments right now. And I want to start with just a simple elementary question. What is the Fragment? Alright. So, we can say, a fragment is a small particle and it's created inside of an activity. But fragments are actually different from normal activities. Fragments have their own life cycle. Fragments are serviced with android 3.0 developers. Fragments provide developers flexible working environment. So, for example, you're going to add more than one fragment inside one activity, so that you can design multiple user interfaces. So, soon we're going to design some examples just like that. Now, we used activity in all of the examples that we built up until now. So, if you think, go back in time, you think about how we constructed those examples? They've always had one user interface.

And users then had to pass to another activity when they wanted to make a different process happen. Well, fragments is going to blow your mind. So, you can show users more than one user interface on the same screen. And all these interfaces can, guess what, run independently because all fragments have their own life cycle. Also, you can use one fragment for more than one activity. You can use one design for different activities. So, it's a terrific tool, gives you flexibility as a developer, and you don't have to design that part  and write its codes out all over again. You can call the fragment in different activities and run it that way. So again, it's thanks to fragments, you can close one fragment and open up a new fragment inside of an activity. All of this makes it so the life cycle of the activity is not spoiled. So, you could consider fragments as modular particles of activities by looking at all that we mentioned before. So, these features provide an improved, flexible, and easy working environment, so that developers can develop better quality applications. Now, one more important detail about fragments. A fragment must run inside an activity. So, in other words, the fragment can't run by itself. The fragment life cycle depends on the activity life cycle. So, if an activity closes, all depending fragments are also closed. So, we can look at this picture if we want example for the usage of fragments. So, Android uses this example on it's official site to explain fragments. Using fragments on tablets gives more advantages because tablets have wider screens, so users can then take advantage of the wider screen. So, I'll show you then in this example, you see, there are two fragments on the tablet screen. So, let's assume that this application is made for reading different articles. So, on the left hand side, there could be articles in list form. So, when the user clicks on an article from that list, the article can be opened on the right hand side fragment. So, that way the user can pick articles from the left side, can read the chosen article and whatever the content is on the right side. Now, if you were born yesterday  and you were just thinking,  "Hey, well, so what's the big deal?" Well, the ability to do more than one process on one screen increases the UX/UI quality of the application. So, our users experience is going up, up, up, up, up.

It's higher. So, without fragments, users would have to choose an article in one activity and then open another activity to read the content of the article. And if the user wants to turn the article list page, well, the article content page, which is inside one activity must be closed and the article list page which is inside another activity must be open. So, you understand right? The same example design is shown on the right hand side for phones. But there's not enough space obviously to show two fragments in one phone screen. So, two different fragments are located inside two different activities. So, when a user selects an article from a list, new activity opens and article content is shown inside the fragment. Now, you can see many, many, many advantages for fragments for this user experience and many different applications. But I do want to show you a different usage example of fragments with running applications. So, here you can see there is an application created with android studio for messaging. Now, that application can be similar to that WhatsApp application. So, in that application, three fragments were used in one activity. The first section is messaging. The second one is this status section. The third fragment is the call section. So, you can see obviously the applications aren't finished, that's why the sections inside are empty. But the important thing here is, there are three fragments inside one activity and all fragments are running independently. So, what does that mean? You don't need to close activities to pass one fragment to another fragment because all fragments have their own lifestyle. So, they all run independently and all fragments have their own user interface. So, for this example in this section, you can chat with your contacts. In this section, you can share your status, and in this other section, you can call one of your contacts. So to me, I get all excited about this because a design like this really increases user experience. You can make that happen just by using fragments and there are so many more examples like that. Alright? So, we're going to go on to learn more about fragments by improving our own applications. Alright? 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