title: Nested fragment in dynamic ViewPager attached to wrong parent
body: <p>I have a <code>ViewPager</code> that can display either four or five tabs (toggleable using "DO STUFF" and "DO STUFF2" button). I am using a <code>FragmentStatePagerAdapter</code> for my <code>ViewPager</code>.
  <a href="https://i.stack.imgur.com/c7czW.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/c7czW.jpg" alt="enter image description here"></a>
  For the "Table" tab only, i added a fragment within it using <code>FragmentTransaction.replace()</code>.
  <a href="https://i.stack.imgur.com/FDb8s.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/FDb8s.jpg" alt="enter image description here"></a></p>
  
  <p>However when i toggle to five tabs the nested fragment is added to BOTH "Squad" tab and "Table" tab. I only want it to be added to "Table" tab. How can i accomplish this?
  <a href="https://i.stack.imgur.com/3mM83.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/3mM83.jpg" alt="enter image description here"></a></p>
  
  <p>Fragment holding our ViewPager:</p>
  
  <pre><code>public class GrandpaFragment extends Fragment implements View.OnClickListener {
      private SectionPagerAdapter mSectionsPagerAdapter;
      private ViewPager mViewPager;       
  
      // Some irrelevant codes
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
          View rootView = inflater.inflate(R.layout.fragment_grandpa, container, false);
          mViewPager = (ViewPager) rootView.findViewById(R.id.pagerTournament);
          mSectionsPagerAdapter = new SectionPagerAdapter(getChildFragmentManager());
          mSectionsPagerAdapter.setViewPager(mViewPager);
          mViewPager.setAdapter(mSectionsPagerAdapter);
  
          btnInfo=(TextView)rootView.findViewById(R.id.btnInfo);
          btnSquad=(TextView)rootView.findViewById(R.id.btnSquad);
          btnTable=(TextView)rootView.findViewById(R.id.btnTable);
          btnFixtures=(TextView)rootView.findViewById(R.id.btnFixtures);
          btnStats=(TextView)rootView.findViewById(R.id.btnStats);
          layoutMainTab=(LinearLayout)rootView.findViewById(R.id.layoutMainTab);
  
          // Set onClickListener
          btnInfo.setOnClickListener(this);
          btnSquad.setOnClickListener(this);
          btnTable.setOnClickListener(this);
          btnFixtures.setOnClickListener(this);
          btnStats.setOnClickListener(this);
  
          setupTabButtonsText();
  
          return rootView;
      }
  
      public void update(){
          mSectionsPagerAdapter.updateAdapterData(5);
  
          setupTabButtonsText();
      }
      public void update2(){
          mSectionsPagerAdapter.updateAdapterData(4);
  
          setupTabButtonsText();
      }
  
      private void setupTabButtonsText(){
          if(mSectionsPagerAdapter.totalTabCount == 4){
              layoutMainTab.setWeightSum(8);
              btnSquad.setText("Table");
              btnTable.setText("Fixture");
              btnFixtures.setText("Stats");
              btnStats.setVisibility(View.GONE);
          }
          else{
              layoutMainTab.setWeightSum(10);
              btnSquad.setText("Squad");
              btnTable.setText("Table");
              btnFixtures.setText("Fixture");
              btnStats.setVisibility(View.VISIBLE);
          }
      }
  
      @Override
      public void onClick(View v) {
          switch (v.getId()) {
              case R.id.btnInfo:
                  mViewPager.setCurrentItem(0);
                  break;
              case R.id.btnSquad:
                  mViewPager.setCurrentItem(1);
                  break;
              case R.id.btnTable:
                  mViewPager.setCurrentItem(2);
                  break;
              case R.id.btnFixtures:
                  mViewPager.setCurrentItem(3);
                  break;
              case R.id.btnStats:
                  mViewPager.setCurrentItem(4);
                  break;
              default:
                  break;
          }
      }
  }
  </code></pre>
  
  <p>Fragment for each screen in ViewPager:</p>
  
  <pre><code>public class PapaFragment extends Fragment implements View.OnClickListener{
      public static final String ARG_SECTION_NUMBER = "section_number";
      private Button button, button2;
  
      public PapaFragment() {
      }
  
      public static PapaFragment newInstance(String sectionNumber) {
          PapaFragment fragment = new PapaFragment();
          Bundle args = new Bundle();
          args.putString(ARG_SECTION_NUMBER, sectionNumber);
          fragment.setArguments(args);
          return fragment;
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
          View rootView = inflater.inflate(R.layout.fragment_papa, container, false);
          TextView textView = (TextView) rootView.findViewById(R.id.section_label);
          textView.setText(getString(R.string.section_format, getArguments().getString(ARG_SECTION_NUMBER)));
          button = (Button) rootView.findViewById(R.id.dostuff);
          button.setOnClickListener(this);
          button2 = (Button) rootView.findViewById(R.id.dostuff2);
          button2.setOnClickListener(this);
  
          return rootView;
      }
  
      @Override
      public void onStart(){
          super.onStart();
          if(getArguments().getString(ARG_SECTION_NUMBER).equals("Table")) {
              SonFragment f = SonFragment.newInstance("A", "B");
              getChildFragmentManager().beginTransaction()
                      .replace(R.id.placeholder, f).commit();
          }
      }
  
      @Override
      public void onClick(View v) {
          if(v.getId() == R.id.dostuff){
              ((GrandpaFragment)getParentFragment()).update();
          }
          else{
              ((GrandpaFragment)getParentFragment()).update2();
          }
  
      }
  }
  </code></pre>
  
  <p>Our FragmentStatePagerAdapter:</p>
  
  <pre><code>public class SectionPagerAdapter extends FragmentStatePagerAdapter {
      private static final int TOTAL_TAB_SQUAD = 5;
      private static final int TOTAL_TAB_NO_SQUAD = 4;
  
      private Context mContext;
      public int totalTabCount = 4;
      private ViewPager mViewPager;
      private FragmentManager fm;
  
      public SectionPagerAdapter(FragmentManager fm) {
          super(fm);
          this.fm = fm;
      }
  
      public void setViewPager(ViewPager viewPager){
          mViewPager = viewPager;
      }
  
      public void updateAdapterData(int count){
          totalTabCount = count;
  
          int currentItem = mViewPager.getCurrentItem();
          mViewPager.setAdapter(this);
          mViewPager.setCurrentItem(currentItem);
  
          notifyDataSetChanged();
      }
  
      @Override
      public Fragment getItem(int position) {
          Fragment fragment=null;
          switch (position) {
              case 0:
                  fragment = PapaFragment.newInstance("InfoDetail");
                  break;
              case 1:
                  if(totalTabCount == TOTAL_TAB_SQUAD) {
                      fragment = PapaFragment.newInstance("Squad");
                  }
                  else {
                      fragment = PapaFragment.newInstance("Table");
                  }
                  break;
              case 2:
                  if(totalTabCount == TOTAL_TAB_SQUAD) {
                      fragment = PapaFragment.newInstance("Table");
                  }
                  else {
                      fragment = PapaFragment.newInstance("Fixture");
                  }
                  break;
              case 3:
                  if(totalTabCount == TOTAL_TAB_SQUAD) {
                      fragment = PapaFragment.newInstance("Fixture");
                  }
                  else {
                      fragment = PapaFragment.newInstance("Stats");
                  }
                  break;
              case 4:
                  fragment = PapaFragment.newInstance("Stats");
                  break;
              default:
                  break;
          }
          return fragment;
      }
  
      @Override
      public int getItemPosition(Object object) {
          return POSITION_NONE;
      }
  
      @Override
      public int getCount() {
          return totalTabCount;
      }
  }
  </code></pre>
  
  <p>Xml for GrandpaFragment</p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="match_parent"
      android:layout_height="match_parent"&gt;
      &lt;RelativeLayout
          android:id="@+id/layoutTournamentTab"
          android:layout_width="match_parent"
          android:layout_height="wrap_content" &gt;
  
          &lt;LinearLayout
              android:id="@+id/layoutMainTab"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="horizontal"
              android:weightSum="10" &gt;
  
              &lt;TextView
                  android:id="@+id/btnInfo"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:layout_weight="2"
                  android:background="@color/colorPrimaryDark"
                  android:layout_margin="1dp"
                  android:gravity="center"
                  android:textColor="@android:color/white"
                  android:text="Info" /&gt;
  
              &lt;TextView
                  android:id="@+id/btnSquad"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:layout_weight="2"
                  android:gravity="center"
                  android:background="@color/colorPrimaryDark"
                  android:layout_margin="1dp"
                  android:textColor="@android:color/white"
                  android:text="Squad"/&gt;
  
              &lt;TextView
                  android:id="@+id/btnTable"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:layout_weight="2"
                  android:gravity="center"
                  android:background="@color/colorPrimaryDark"
                  android:layout_margin="1dp"
                  android:textColor="@android:color/white"
                  android:text="Table" /&gt;
              &lt;TextView
                  android:id="@+id/btnFixtures"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:layout_weight="2"
                  android:gravity="center"
                  android:background="@color/colorPrimaryDark"
                  android:layout_margin="1dp"
                  android:textColor="@android:color/white"
                  android:text="Fixture" /&gt;
              &lt;TextView
                  android:id="@+id/btnStats"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:layout_weight="2"
                  android:gravity="center"
                  android:background="@color/colorPrimaryDark"
                  android:layout_margin="1dp"
                  android:textColor="@android:color/white"
                  android:text="Stats"/&gt;
          &lt;/LinearLayout&gt;
      &lt;/RelativeLayout&gt;
  
  
      &lt;android.support.v4.view.ViewPager
          android:id="@+id/pagerTournament"
          android:layout_width="fill_parent"
          android:layout_height="match_parent"&gt;
  
      &lt;/android.support.v4.view.ViewPager&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>Xml for PapaFragment:</p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:paddingBottom="@dimen/activity_vertical_margin"
      android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"&gt;
  
      &lt;TextView
          android:id="@+id/section_label"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:textSize="30sp"/&gt;
  
      &lt;Button
          android:layout_below="@id/section_label"
          android:id="@+id/dostuff"
          android:text="do stuff"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" /&gt;
  
      &lt;Button
          android:layout_below="@id/section_label"
          android:layout_toRightOf="@id/dostuff"
          android:id="@+id/dostuff2"
          android:text="do stuff2"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" /&gt;
  
  
      &lt;RelativeLayout android:id="@+id/placeholder"
          android:layout_below="@id/dostuff"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:background="@color/colorAccent"/&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>