title: TabLayout - ViewPager - Fragment
body: <p>I have a problem with my ViewPager and fragments it contains. I have 2 menus (A and B), my homepage is directly A. When I click on a menu B fragment which contains a TabLayout, which contains a ViewPager himself three fragments (each contains a simple TextView with Lorem ipsum).
  The 3 fragments of ViewPager are correct but if I click on the menu A and I click again on the menu B I do not have any content. Nothing on the fragments 1 and 2 against with the 3rd still has the text and if I return to fragment 1 reads income (for nothing against the fragment 2).</p>
  
  <p>Here is my code :</p>
  
  <p><strong>Menu B (FragmentTabLayout)</strong></p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>public class TabLayoutFragment extends Fragment {
  
      private static final String ARG_TEXT = "ARG_TEXT";
      private static final String ARG_COLOR = "ARG_COLOR";
  
      private Toolbar toolbar;
      private TabLayout tabLayout;
      private ViewPager viewPager;
  
      public TabLayoutFragment() {
          // Required empty public constructor
      }
  
      public static TabLayoutFragment newInstance(String text, int color) {
          Bundle args = new Bundle();
          args.putString(ARG_TEXT, text);
          args.putInt(ARG_COLOR, color);
  
          TabLayoutFragment tabLayoutFragment = new TabLayoutFragment();
          tabLayoutFragment.setArguments(args);
  
          return tabLayoutFragment;
      }
  
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
  
          setHasOptionsMenu(true);
          // Inflate the layout for this fragment
          View view =  inflater.inflate(R.layout.fragment_tab_layout, container, false);
  
          toolbar = (Toolbar) view.findViewById(R.id.toolbar);
          ((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
          toolbar.setBackgroundColor(getArguments().getInt(ARG_COLOR));
          toolbar.setTitle(getArguments().getString(ARG_TEXT));
  
          viewPager = (ViewPager) view.findViewById(R.id.viewpager);
          setupViewPager(viewPager);
  
  
          tabLayout = (TabLayout) view.findViewById(R.id.tabs);
          tabLayout.setupWithViewPager(viewPager);
          tabLayout.setBackgroundColor(getArguments().getInt(ARG_COLOR));
          tabLayout.setSelectedTabIndicatorColor(Color.WHITE);
          
  
          return view;
      }
  
      @Override
      public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
          inflater.inflate(R.menu.tab_menu, menu);
          super.onCreateOptionsMenu(menu, inflater);
      }
  
      private void setupViewPager(ViewPager viewPager) {
          ViewPagerAdapter adapter = new ViewPagerAdapter(getActivity().getSupportFragmentManager());
          viewPager.setAdapter(adapter);
      }
  
      class ViewPagerAdapter extends FragmentPagerAdapter {
  
          //private final List&lt;TabFragment&gt; mFragmentList = new ArrayList&lt;&gt;();
          //private final List&lt;String&gt; mFragmentTitleList = new ArrayList&lt;&gt;();
  
          public ViewPagerAdapter(FragmentManager manager) {
              super(manager);
          }
        
          @Override
          public Fragment getItem(int position) {
              switch (position) {
                  case 0:
                      return TabFragment.newInstance("Fragment 2-1");
                  case 1:
                      return TabFragment.newInstance("Fragment 2-2");
                  case 2:
                      return TabFragment.newInstance("Fragment 2-3");
                  default:
                      return TabFragment.newInstance("Fragment Default");
              }
          }
  
          @Override
          public int getCount() {
              //return mFragmentList.size();
              return 3;
          }
      }
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p><strong>Fragment in ViewPager :</strong></p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>public class TabFragment extends Fragment {
  
      private static final String ARG_TEXT = "ARG_TEXT";
  
      private TextView tv;
  
      public TabFragment() {
          // Required empty public constructor
      }
  
      public static TabFragment newInstance(String text) {
          Bundle args = new Bundle();
          args.putString(ARG_TEXT, text);
  
          TabFragment tabFragment= new TabFragment();
          tabFragment.setArguments(args);
  
          return tabFragment;
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
          // Inflate the layout for this fragment
          View view = inflater.inflate(R.layout.fragment_tab, container, false);
  
          tv = (TextView) view.findViewById(R.id.tv);
  
          return view;
      }
  
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p><strong>Layout FragmentTabLayout :</strong></p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;android.support.design.widget.CoordinatorLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical"&gt;
  
      &lt;android.support.design.widget.AppBarLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"&gt;
  
          &lt;android.support.v7.widget.Toolbar
              android:id="@+id/toolbar"
              android:layout_width="match_parent"
              android:layout_height="?attr/actionBarSize"
              android:background="?attr/colorPrimary"
              app:layout_scrollFlags="scroll|enterAlways"
              app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /&gt;
  
          &lt;android.support.design.widget.TabLayout
              android:id="@+id/tabs"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              app:tabMode="fixed"
              app:tabGravity="fill"/&gt;
      &lt;/android.support.design.widget.AppBarLayout&gt;
  
      &lt;android.support.v4.view.ViewPager
          android:id="@+id/viewpager"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          /&gt;
  
  &lt;/android.support.design.widget.CoordinatorLayout&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p><strong>layout tabFragment :</strong></p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;LinearLayout 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"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      android:orientation="vertical"
      tools:context="com.application.myapplication.TabFragment"&gt;
  
      &lt;android.support.v4.widget.NestedScrollView
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          app:layout_behavior="@string/appbar_scrolling_view_behavior"
          &gt;
  
      &lt;TextView
          android:id="@+id/tv"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:text="@string/lorem_ipsum"
          /&gt;
  
      &lt;/android.support.v4.widget.NestedScrollView&gt;
  
  &lt;/LinearLayout&gt;</code></pre>
  </div>
  </div>
  </p>