I have a Fragment stack like this
F1 -> F2 -> F3 -> F4 -> F5
Now I need to remove F2, F3, F4 fragments.
I need if i press back button from F5 fragment, it should be go to F1.
NOTE: I am not changing fragment fragment from activity. changing fragment from fragment.
On destroy of the Fragment
F5
clear Back Stack
upto F2
.
Try something like this:
public
method in your MainActivity
:
public void clearBackStackInclusive(String tag) {
getSupportFragmentManager().popBackStack(tag, FragmentManager.POP_BACK_STACK_INCLUSIVE);
}
now in your F5
fragment:
@Override
public void onDestroy() {
super.onDestroy();
((MainActivity)getActivity()).clearBackStackInclusive("tag"); // tag (addToBackStack tag) should be the same which was used while transacting the F2 fragment
}
Reference
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With