Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How we can Remove Dynamically added Fragments from layout

Can any one help me. I have a Fragment say FRAGMENT A and am adding it to a layout dynamically...Suppose i have added 3 instance of FRAGMENT A to that layout.Then How i can Remove that 3 Fragment instance programmatically.I tried google searches and also another stackoverflow threads but they are not working..

PLease help me

Thank you

like image 991
Sam Avatar asked Apr 02 '13 10:04

Sam


1 Answers

it's actually pretty simple:

let's say you added the fragment like this:

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

instead, you'll add it with a TAG too

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

then to remove:

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

happy coding =]

like image 172
Budius Avatar answered Oct 07 '22 00:10

Budius