Q:

fragment not being removed

After many days of trying to find a solution for having a dual pane fragment along with tabs, i gave up and made my own listview that's in a an x-large layout for tablets, and having only fragments to manage my tabs. All my code works except one part, where i am trying to remove the old fragment in the tab and create a new fragment based on the item clicked in the list. 
Here is where the fragments for the tabs are generated:
public class AppSectionsPagerAdapter extends FragmentPagerAdapter {
   public AppSectionsPagerAdapter(FragmentManager fm) {
       super(fm);                                 
    }
   //THESE FRAGMENTS ARE GENERATED WITH TABS AT START

 @Override
 public Fragment getItem(int i) {
     switch (i) {     
     case 0:
            return new DescriptionFragment();
     case 1:
      return new ImagesFragment();
     default:
            return new DescriptionFragment();
  }
}

where i add fragments from a listview click:
public  void addfragment() {   
//THIS FRAGMENT GOES ON TOP AFTER LISTVIEW CLICK
    kick_activity.this.getSupportFragmentManager().popBackStack();
    DescriptionFragment fragment = new DescriptionFragment();
    android.support.v4.app.FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.replace(R.id.container, fragment);
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    ft.commit();
}

my DescriptionFragment fragment: 
 public static class DescriptionFragment extends Fragment {
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_section_description, container, false);  
        text = ((TextView) rootView.findViewById(R.id.description));         
        text.setText(Html.fromHtml(description_text));
        return rootView;
 }

I know what i'm doing wrong, I just don't know how to fix it. It seems that the fragment from getItem() is created and when i click on my listview and addfragment() is called, it overlays due to the fragments not being linked. What can i place in 'case 0' instead of 'return new DescriptionFragment' so that all my fragments are linked and don't overlay?
Note - when i try and use ft.repalce in getItem() the app crashes and according to the LogCat, it cannot find R.id.container, so i cannot simply move that code in.
Thanks in advance for your help, i will greatly appreciate any solutions as I have been stuck on this for quite a few days.

A:

If I understand correctly for each list item you want to create / set a corresponding Detail and Image fragment which will be hosted in the FragmentPagerAdapter?
If so, when a list item is clicked you could call:-
mPager.setAdapter(new MyAdapter(getSupportFragmentManager(), listItemPosition);

When a list item is clicked, and let the FragmentPagerAdapter manage the fragments.
You'd also need to change the adapter to take the list item position and use accordingly. Such as:-
public static class MyAdapter extends FragmentPagerAdapter {
    private int mPosition;
    public MyAdapter(FragmentManager fm, int position) {
        super(fm);
        mPosition = position;
    }
    ...
}

