Currently, I have one action, and fragments are added to it (search, information about songs, settings, etc.). I implemented navigation based on the sidebars, so now, as a side effect, there are no restrictions on how many fragments are added to Backstack. Is it possible to limit the number of fragments or delete old records? Each fragment of a songโs fragments has a recommended list of songs, and thanks to this, you can go to a fragment of fragments of other fragments. You can easily have 30 fragments in the backstack, which, if you have DDMS open, you can slowly see (but correctly) the size of the heap.
Edit: One thing I was trying to do was if the user clicked one of the side menu options, if this fragment is already in the stack, try to return to this fragment instead of creating a new one, but, of course, if the user is on the "About song, "then he expected that pushing back would lead him to this fragment so that it would not work.
Edit 2: This is my addFragment method (along with Phil's suggestion):
public void addFragment(Fragment fragment) { FragmentManager fm = getSupportFragmentManager(); if(fm.getBackStackEntryCount() > 2) { fm.popBackStack(); } fm.beginTransaction() .replace(R.id.fragment_container, fragment).addToBackStack("") .commit(); }
I just tried, and assuming my fragment history: A-> B-> C-> D, returning from D, exits B-> A-> exit.
I just went to 8 levels to check: A-> B-> C-> D-> E-> F-> G-> H, and, returning from H, the same thing happened: H-> B-> A-> exit.
All fragments are added through this method above. I would like to see: H-> G-> F-> exit.