Requirement scenarios:
The following scenario is encountered during development:
1. Create Fragment A. In this case, the stack structure is:
1.Fragment A
Copy the code
2. Create Fragment B and display it. From bottom to top, the stack looks like this:
2.Fragment B
Copy the code
1.Fragment A
Copy the code
3. Create Fragment C and display it. From bottom to top, the stack looks like this:
3.Fragment C
Copy the code
2.Fragment B
Copy the code
1.Fragment A
Copy the code
So, how to jump from Fragment C to Fragment A all at once?
Implementation:
When pushing, use the method
AddToBackStack (nameForBackstackstate) passes in a parameter name, which is an indication of Fregment and will be used next.Copy the code
Code:
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
String name = fragment.getClass().getName();
fragmentTransaction.replace(R.id.container, fragment).addToBackStack(nameForBackstackstate).commit();
Copy the code
Do this when popping up:
fragmentManager.popBackStackImmediate(FragmentA.class.getName() , FragmentManager.POP_BACK_STACK_INCLUSIVE);
Copy the code
The second argument to this method
POP_BACK_STACK_INCLUSIVE indicates that this pop-up behavior is to stack all fragments above the Fragment of "specified name" at once.Copy the code
Reference:
www.cnblogs.com/qixing/p/40…