Suppose I have 3 Fragments: A,B,C
Now by pressing Back Button, Navigation should be C->B->A -> finish
In other words, irrespective of number of navigation (without pressing back button) between B->C and C->B, back navigation by pressing back button should in order C->B->A
Assuming that all the allowed transitions are A<->B<->C, you can use FragmentManager.popBackStack()
method when navigating C->B, and always add the transition to back stack when navigating B->C.
Here's how to do the transitions:
public void transitionFromAToB() {
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container, new FragmentB())
.addToBackStack(null)
.commit();
}
public void transitionFromBToC() {
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container, new FragmentC())
.addToBackStack(null)
.commit();
}
public void tansitionFromCToB() {
getSupportFragmentManager().popBackStack();
}
public void transitionFromBToA() {
getSupportFragmentManager().popBackStack();
}
So when transitioning C->B, you're not actually doing a new transition, you're actually just reversing the B->C transition.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.