How can we remove dynamically added fragments from the layout

Can someone help me. I have a fragment, say FRAGMENT A, and I add it to the layout dynamically ... Suppose I add 3 instances of FRAGMENT A to this layout. Then how can I delete this 3 instance of the fragment programmatically. I tried google search as well as another stackoverflow but they do not work.

Plase help me

thank

+5
source share
1 answer

This is actually quite simple:

let's say you added a snippet as follows:

fragmentTransac.add(R.id.content, fragA);

instead, you add it also with TAG

fragmentTransac.add(R.id.content, new FragA(), "first");
// then the other
fragmentTransac.add(R.id.content, new FragA(), "second");

then delete:

Fragment f = getFragmentManager().findFragmentByTag("first");
if(f!=null) fragmentTransac.remove(f);
fragmentTransac.commit();

happy coding =]

+17
source

All Articles