What is addToBackStack in Fragment
Jun 13, 2020
Fragment API unlike Activity API does not come with Back Button navigation by default. So if you have added or replaced many Fragments on a Fragment container. There is no way to go back to the previous Fragment on a Back Button Pressed.
Let's see the difference between the behaviors
Case 1: Default behavior
getSupportFragmentManager()
.beginTransaction()
.add(R.id.fragmentContainer, fragment, "TAG")
.commit();
Case 2: With addToBackStack
getSupportFragmentManager()
.beginTransaction()
.add(R.id.fragmentContainer, fragment, "TAG")
.addToBackStack("TAG")
.commit();
Putting up content takes a lot of time and effort π.
π clap π If you think you learned something from this article.You can find me on Stackoverflow,Github,and,on Linkedin.
Give feedback and feel free to correct mistakes.
Read More:
How to slide in a fragment from the Bottom.