What is addToBackStack in Fragment

Rohit Singh
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.

--

--

Rohit Singh

Android Developer | Arizona State University | NASA Psyche Research Aide