Requirement scenarios:

The following scenario is encountered during development:

A. Create Fragment A to display. In this case, the stack structure is:

1.Fragment A 
Copy the code

B. Create Fragment B and display it. From bottom to top, the stack looks like this:

2.Fragment B
1.Fragment A
Copy the code

C. Create Fragment C and display it. From bottom to top, the stack looks like this:

3.Fragment C
2.Fragment B
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) 
Copy the code

We pass in a parameter name, which is an identifier for Fregment, which we will use next. 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); The second argument to this method

POP_BACK_STACK_INCLUSIVE
Copy the code

Indicates that this pop-up behavior is to push all fragments above the Fragment of “specified name” at once.

Reference:

www.cnblogs.com/qixing/p/40…