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();
            }
        });

Related posts

Recent Viewed