How to implement unlimited fragment stack in android like Facebook
application where you navigate from one profile to another and when you press the back button it navigates back to each profile you visited.

- 12,161
- 7
- 47
- 78
2 Answers
What you need is adding each new fragment you created to backstack as following, and then when you press back on new fragments, you just need to pop it from stack:
Fragment fragment = new YourFragmentGoesHere();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment)
.addToBackStack("Your Pre-defined String name for related fragment").commit();
And when you press back in new fragment you go backwards with following :
FragmentManager fm = getFragmentManager();
fm.popBackStack("Your pre-defined string name for fragment", FragmentManager.POP_BACK_STACK_INCLUSIVE);
That's it basically.

- 713
- 5
- 21
It seems that you're looking for implementing an unlimited back stack for fragments in Android. What you really want to do is first read this page: Providing Proper Back Navigation
Pay particular attention to the "Implement Back Navigation for Fragments" section. I could just repeat all the information here, but I'll instead specify that yes, this is technically unlimited, in the same sense Facebook's back button functionality is unlimited (ie, limited by free memory).
As another note, pay careful attention if different fragments change the UI in certain ways, as the standard back navigation for fragments just reverses the exact fragment transaction you had. If you run into issues, make sure to properly override the onBackPressed method and/or try to add/remove/replace fragments differently (ex: this question here).

- 1
- 1

- 1,846
- 3
- 20
- 28