Limiting the number of fragments in backStack?

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.

+6
source share
2 answers

You can programmatically control the number of fragments in BackStack:

 FragmentManager fm = getActivity().getSupportFragmentManager(); if(fm.getBackStackEntryCount() > 10) { fm.popBackStack(); // remove one (you can also remove more) } 

Just check how many fragments there are in your Backstack and delete if there is an โ€œoverflowโ€.

If you want to remove specific fragments from BackStack, you will have to implement your own BackStack and override onBackPressed (). Since the Fragment BackStack is a stack (as indicated in the title), you can delete only the top element (the last one added), there is no way to remove fragments between them.

You can use, for example,

 ArrayList<Fragment> 

to implement your own stack. Just add and remove fragments from this "stack" (this is no longer a full stack) whenever you want and handle loading previous fragments by overriding the onBackPressed () method.

+10
source

This is an old question, but my answer may help someone.

Why not check if the fragment is on the stack and place it? This way, you donโ€™t have to worry about the size of the back stack (if you have many fragments).

  String backStateName = fragment.getClass().getName(); boolean fragmentPopped = false; try { // true if fragment is in the stack fragmentPopped = fragmentManager.popBackStackImmediate(backStateName, 0); } catch (Exception e) { e.printStackTrace(); } FragmentTransaction ft = fragmentManager.beginTransaction(); if ( !fragmentPopped ) { //fragment not in back stack, add it... ft.setTransition( FragmentTransaction.TRANSIT_FRAGMENT_FADE ); ft.replace(R.id.main, fragment); ft.addToBackStack( backStateName ); try { ft.commit(); } catch (Exception e) { e.printStackTrace(); } } 
+3
source

Source: https://habr.com/ru/post/951939/


All Articles