Fragment Backstack - Part 2

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.


So now, let's deal with Backstack for fragments. So, in Android Studio we can use one of the examples that we worked on before. In this example there is a main activity, frame layout, and button. When I opened the main activity first, the first fragment is running inside the frame layout. And when I click the button, the second fragment is shown inside the frame layout instead of the first fragment.

So, if we look at the Kotlin code inside the main activity. First, the first fragment is running, then the second fragment runs inside the buttons, click listener. Now have a look at this, there's no finish button in here. So, if you think the fragment is an activity, when we open the second fragment, the first fragment must be out of the stack. Because we didn't close the first fragment with code. So, why don't we run the application and see, and we can see how the Backstack method works with fragments. So, the application opened, and the first fragment starts to run just by opening and after clicking the button, the second fragment also opens. When I click on the back button, the application is closed. So in fact, first fragment must be open. That means the Backstack system doesn't work on fragments. In other words when we open a new fragment, the old fragment doesn't go to the stack. So, that means that I've got to have a process to add to the stack with code. So, let's go back and have a look at the main activity code again. Here, while the second fragment is running, the first fragment closes and as you may remember an activity, that process was completely the opposite, activity was not closing by itself. So, now I will add the first fragment to stack with code. I the second fragment opening section after the add method, I will write seconFragmentTransaction.add to Backstack, inside parenthesis I'll write null. So, that method will run the stack empty. So, by using this because the stack system won't work, the first fragment won't close and it will be added to the stack. That means I've got to make a process to keep the old fragment in stack, while opening up a new one. So, now let's run the application again. Application opens, first fragment is running and I'm opening the second fragment. Second fragment is also opening and after clicking the 'Back' button, the first fragment is open. Because the first fragment was added back to the stack. Just by clicking the back button, it carries it from the stack. Now stack is empty. When I click the back button again, the application closes. So, if you use more than two fragments, let's say five fragments, you can add fragments into stack however much you want to just by writing the code, and sure, there you go, that's backstack and fragments. 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