title: Add ViewPager in Fragment in Android
body: <p>I have two fragments: <code>FirstFragment</code> and <code>SecondFragment</code>. <code>FirstFragment</code> contains <code>ViewPager</code>, which has three pages (three fragments). In the <code>onCreate</code> of activty, I add <code>FirstFragment</code> to main layout, and three pages show normally. </p>
  
  <p>Problem is: when I replace <code>FirstFragment</code> by <code>SecondFragment</code>, and then comeback <code>FirstFragment</code> three pages disappear. Then I scroll <code>ViewPager</code> forward and backward, pages show but only first and third pages show, second page disappear.</p>
  
  <p>Here <code>FirstFragment</code>:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>package com.example.bkmsx.afragment;
  
  import android.os.Bundle;
  import android.support.annotation.Nullable;
  import android.support.v4.app.Fragment;
  import android.support.v4.app.FragmentManager;
  import android.support.v4.app.FragmentPagerAdapter;
  import android.support.v4.view.ViewPager;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  
  public class FirstFragment extends Fragment {
      ViewPager mViewPager;
      static MainActivity mainActivity;
      public static FirstFragment newInstance(MainActivity activity){
          mainActivity = activity;
          return new FirstFragment();
      }
      @Nullable
      @Override
      public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
          View view = inflater.inflate(R.layout.first_fragment, null);
          mViewPager = (ViewPager) view.findViewById(R.id.view_pager);
          ViewPagerAdapter adapter = new ViewPagerAdapter(mainActivity.getSupportFragmentManager());
          mViewPager.setAdapter(adapter);
          return view;
      }
  
      private class ViewPagerAdapter extends FragmentPagerAdapter {
          public ViewPagerAdapter(FragmentManager fm) {
              super(fm);
          }
  
          @Override
          public Fragment getItem(int position) {
              String text = "";
              switch (position) {
                  case 0: text = "First Page";
                      break;
                  case 1: text = "Second Page";
                      break;
                  case 2: text = "Third Page";
                      break;
              }
              return TextFragment.newInstance(mainActivity, text);
          }
  
          @Override
          public int getCount() {
              return 3;
          }
      }
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p><code>SecondFragment</code>:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>package com.example.bkmsx.afragment;
  
  import android.os.Bundle;
  import android.support.annotation.Nullable;
  import android.support.v4.app.Fragment;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.TextView;
  
  public class SecondFragment extends Fragment {
      static MainActivity mainActivity;
  
      public static SecondFragment newInstance(MainActivity activity) {
          mainActivity = activity;
          return new SecondFragment();
      }
      @Nullable
      @Override
      public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
          TextView textView = new TextView(mainActivity);
          textView.setText("New Fragment");
          return textView;
      }
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>Any suggestion? Thanks in advance.</p>