Fragment Backstack - Part 1

Start course
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.


Hi everyone. In this video, I want to introduce you to something pretty cool. It's called Fragment Backstack. Now, the subject of Backstack is a process when clicking the 'Back' button in an application or activity, you may have heard of it before. So, let me show you how it works here. We'll pass on over to Android Studio and we'll dispense with theory and get right down to it. Now, before we start passing Backstack and Fragment, I want to talk about Backstack in Activity. So first, let's see over running application. On that application there is an activity and that's our first activity. Here there's a button to pass the Second Activity. So, when I click that button the Second Activity opens. So here, the Second Activity is open, but what happened to the First Activity?

Does the First Activity get closed? What happens?

So here, when we click on the back button, the application is closed and that means the First Activity gets closed as well because we got out of that application. So, while passing from the First Activity to the Second Activity, if you don't write closing code for the First Activity, Android Studio doesn't close that Activity automatically. So in other words, if you don't write closing code for the First Activity, when you pass to the Second Activity, the First Activity won't close. The First Activity will be added to stack. Remember stacks, right? If you go way back to your data structure lessons, you might remember the concept. It's just another way of entering data so it can be used. So, here's a good way to look at stack structure that it gets put into a container, and then the first data that gets entered drops to the bottom and any additions get put on top. The fun will start when you start to unload the data, the first entered data will exit last or file up. So now, the unloading process starts with the top most data. That's why we call it first in, last out.

So, this data structure concept also applies to Activity. If you open Activities again and again and if you don't close them, they will be added to stack. So, after you click the 'Back' button, you'll start to see Activities in the queue from last to first. For example, you open up the First Activity first, then you open the Second Activity by using the button in the First Activity. And then in this case, the First Activity added the lowest of the stack. And then after you pass from the Second Activity  to Third Activity, the Second Activity will be located over the First Activity in stack. And when you click on the 'Back' button in the Third Activity, you will see the Second Activity comes from the stack. Now, if you click the 'Back' button in the Second Activity again, you'll see the First Activity comes from the stack. So, in that way we come back from the Third Activity to the First Activity.

So, if we look over our Android Studio code, in the First Activity, there is a button, and over that button there is some writing here that will say 'Go to Second Activity'. So, if we have a look at the Kotlin code of that Activity, I defined that button at first in here and I added a click listener to the button and inside that click listener, I wrote the necessary code to open the Second Activity with the method of intent. So, let's have a look at this Second Activity. And here is just a text view and Second Activity is writing over it. So, let's have a look at the Second activity's Kotlin code and there's nothing written here. It's empty just waiting for us. So, let's run the application and have a look at the logic. And yeah, here we have the application opens. Second Activity opens after I click the 'Go to Second Activity' button. Now, the First Activity added to the stack just by opening of the Second Activity. So, when I click on the 'Back' button, stack gets control because there's only one Activity in stack. We'll just see the First Activity. Now, if I click the 'Back' button again, stack gets control and the application is closed because stack is empty,

there is no Activity to open. All right? So, that's what we call Backstack. Now, if we want to we can control that Backstack. So, if I add a Finish method, after the click listener in the Main Activity, that Activity won't be added to the stack and it will close. So, let's run the application again. Application opens, click the button again. So now, when I click the 'Back' button in the Second Activity, the application closes because the First Activity didn't add to the stack. That's why the First Activity didn't come back, right? But also I can overwrite the Back button and direct it as I want. In Android Studio, there's a method named onBackPress. So, this is the method that controls the Back button. So, by using this method, I can control the process of the Back button in a few different ways. So right now, let's overwrite onBackPress in the Second Activity. And here I will create a new Intent inside the method on background. First, I will make the Activity open by using the Intent method. In fact, when I click the 'Back' button, the First Activity will not open because I closed it.

But by overwriting the Back button with a new Intent method, I'll be able to open that First Activity once again. So now, let's run the application again. It's opened. I'll click the button and pass the Second Activity. Now, the First Activity gets closed by the Finish method, and by clicking the 'Back' button, the First Activity opens. Now, that's the basic practice and theory of Backstack in Activities. But does that system work in fragments, you might wonder. And if so, how? I'll show you.


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