how I can pop specific fragment from stack and remove others from a fragment? for example these are my fragments and I'm in E right know.
A-> B -> C -> D ->E
wanna back from E to B and Clear C and D. How I can do this?
how I can pop specific fragment from stack and remove others from a fragment? for example these are my fragments and I'm in E right know.
A-> B -> C -> D ->E
wanna back from E to B and Clear C and D. How I can do this?
You can add a tag to each fragment while adding them to the backstack and then popfragment from backstack till the fragment with the tag you want is not reached.
FragmentManager fm = getFragmentManager();
for (int i = fm.getBackStackEntryCount() - 1; i > 0; i--) {
if (!fm.getBackStackEntryAt(i).getName().equalsIgnoreCase(tagname)) {
fm.popBackStack();
}
else
{
break;
}
}
Use the can call the function below while you are in Fragment E
:
getFragmentManager().popBackStack("tag", 0);
Here the tag
is string passed as tag while adding fragment B
to backstack
.
Use following code for pop back stack entry:
FragmentManager fm = getSupportFragmentManager();
if (fm.getBackStackEntryCount() > 0) {
fm.popBackStack();
}else {
super.onBackPressed();
}