Android : Destroy previous fragments from layout

on Saturday, August 9, 2014


I have a layout which I populate with two fragments. I have written a service that fetches data online and then repopulate the layout with two fragments . I want to destroy old fragments and then place the new one but this keeps on adding the new fragments in the layout. If I use beginTransaction().replace that only adds one fragment at a time in layout . How can I achieve this thing?



new Handler().post(new Runnable() {
public void run() {

Fragment f = getFragmentManager().findFragmentByTag("fragmentNowPlaying");
if (f != null)
transaction.remove(f);

f = getFragmentManager().findFragmentByTag("fragmentChannel");
if (f != null)
transaction.remove(f);

FragmentTransaction transaction = fm.beginTransaction();
FragmentNowPlaying fragmentNowPlaying = new FragmentNowPlaying();
transaction = fm.beginTransaction().add(R.id.content,fragmentNowPlaying, "fragmentNowPlaying");
transaction.commitAllowingStateLoss();

transaction = fm.beginTransaction();
FragmentChannel fragmentchannel = new FragmentChannel();
transaction = fm.beginTransaction().add(R.id.content,fragmentchannel, "fragmentChannel");
transaction.commitAllowingStateLoss();
}
});

0 comments:

Post a Comment