Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manage BackStackEntries in FragmentManager

How to clear BackStackEntries in FragmentManager ? This is the code I use to change my fragment object:

FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
Fragment homeFragment = new Home();
fragmentTransaction.replace(R.id.mainFragement, homeFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();

In a particular situation I need to remove the complete backstack entries. I didn't find any particular method for that in fragment manager. Do you have any ideas?

like image 668
Labeeb Panampullan Avatar asked Mar 15 '11 06:03

Labeeb Panampullan


1 Answers

I have been working on a method that I think does what you're asking to do, but the only thing I have been able to figure out from the documentation is whether or not this method just empties the BackStack or if there's any implication on the View container. Basically, it finds the entry located at the top of the stack and deletes everything, including that entry. If someone else thinks that there are negative implications let me know because I'd be very curious:

public static void clearBackStack(FragmentManager manager){
    int rootFragment = manager.getBackStackEntryAt(0).getId();
    manager.popBackStack(rootFragment, FragmentManager.POP_BACK_STACK_INCLUSIVE);
}
like image 138
promethium Avatar answered Sep 18 '22 16:09

promethium