Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pop specific fragment from stack and remove others

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?

like image 960
Mahdi Avatar asked Aug 09 '16 14:08

Mahdi


1 Answers

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;
    }
}
like image 110
Smit Davda Avatar answered Oct 04 '22 02:10

Smit Davda