title: Changing fragments in tabs in tablayout during runtime
body: <p>I wonder if somebody could help me. I need to change fragment within individual tabs during runtime. I know it should be done using FragmentPagerAdapter, but that's all I have managed to find out about it. I'm using android studio. My project is running on API 22. Thanks for all answers!
  Edit: Here's the code. </p>
  
  <p>Here I set the tabbed layout class</p>
  
  <pre><code>Stack&lt;Fragment&gt; Tab1;
  Stack&lt;Fragment&gt; Tab2;
  Stack&lt;Fragment&gt; Tab3;
  
  private SectionsPagerAdapter mSectionsPagerAdapter;
  
  private ViewPager mViewPager;
  
  @Override
  protected void onCreate(Bundle savedInstanceState){
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_app_root);
  
      /*Tab1 = new Stack&lt;Fragment&gt;();
      Tab1.addElement(new Tab1Informations());
      Tab2 = new Stack&lt;Fragment&gt;();
      Tab2.addElement(new Tab2Events());
      Tab3 = new Stack&lt;Fragment&gt;();
      Tab3.addElement(new tab3tests());*/
  
      // Create the adapter that will return a fragment for each of the three
      // primary sections of the activity.
      //mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(), Tab1.peek(), Tab2.peek(), Tab3.peek());
      mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(),new Tab1Informations(), new Tab2Events(), new tab3tests());
      // Set up the ViewPager with the sections adapter.
      mViewPager = (ViewPager) findViewById(R.id.container);
      mViewPager.setAdapter(mSectionsPagerAdapter);
  
      TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
  
      mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
      tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
  
      CreateButtonListener();
  }
  </code></pre>
  
  <p>Here I tried to change the SectionChangedAdapter</p>
  
  <pre><code>Stack&lt;Fragment&gt; Tab1;
  Stack&lt;Fragment&gt; Tab2;
  Stack&lt;Fragment&gt; Tab3;
  
  /**
   * The {@link android.support.v4.view.PagerAdapter} that will provide
   * fragments for each of the sections. We use a
   * {@link FragmentPagerAdapter} derivative, which will keep every
   * loaded fragment in memory. If this becomes too memory intensive, it
   * may be best to switch to a
   * {@link android.support.v4.app.FragmentStatePagerAdapter}.
   */
  private SectionsPagerAdapter mSectionsPagerAdapter;
  
  private ViewPager mViewPager;
  
  @Override
  protected void onCreate(Bundle savedInstanceState){
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_app_root);
  
      /*Tab1 = new Stack&lt;Fragment&gt;();
      Tab1.addElement(new Tab1Informations());
      Tab2 = new Stack&lt;Fragment&gt;();
      Tab2.addElement(new Tab2Events());
      Tab3 = new Stack&lt;Fragment&gt;();
      Tab3.addElement(new tab3tests());*/
      //mSectionsPagerAdapter = new 
       SectionsPagerAdapter(getSupportFragmentManager(), Tab1.peek(), 
       Tab2.peek(), Tab3.peek());
      mSectionsPagerAdapter = new 
     SectionsPagerAdapter(getSupportFragmentManager(),new Tab1Informations(), 
      new Tab2Events(), new tab3tests());
      // Set up the ViewPager with the sections adapter.
      mViewPager = (ViewPager) findViewById(R.id.container);
      mViewPager.setAdapter(mSectionsPagerAdapter);
  
      TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
  
      mViewPager.addOnPageChangeListener(new 
      TabLayout.TabLayoutOnPageChangeListener(tabLayout));
      tabLayout.addOnTabSelectedListener(new 
      TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
  
      CreateButtonListener();
  }
  </code></pre>
  
  <p>And here I defined the adapter itself.
                     public class SectionsPagerAdapter extends 
                     FragmentPagerAdapter{
                     Fragment tab1;
                     Fragment tab2;
                     Fragment tab3;</p>
  
  <pre><code>    public SectionsPagerAdapter(FragmentManager fm, Fragment tab1, Fragment 
      tab2, Fragment tab3){
          super(fm);
          this.tab1 = tab1;
          this.tab2 = tab2;
          this.tab3 = tab3;
      }
  
      @Override
      public Fragment getItem(int position){
          switch(position){
              case 0:
                  return tab1;
              case 1:
                  return tab2;
              case 2:
                  return tab3;
              default:
                  return null;
          }
      }
  
      @Override
      public int getCount(){
          // Show 3 total pages.
          return 3;
      }
  
      @Override
      public CharSequence getPageTitle(int position){
          switch(position){
              case 0:
                  return getString(R.string.InformationsTabTitle);
              case 1:
                  return getString(R.string.EventsTabTitle);
              case 2:
                  return getString(R.string.TestsTabTitle);
              default:
                  return null;
          }
      }
  }`
  </code></pre>