title: Same fragment with different contents in viewpager
body: <p>I have one parent fragment where there is viewpager and tablayout.</p>
  <p>Viewpager holds the fragment with different contents associated with it.</p>
  <p>Every think works fine on swipe but whenever I tap on the different tabs of the tablayout, categoryId and categoryName of SwipableFragment is changed to adjacent tab's SwipableFragmet to the visible fragment.</p>
  <p><strong>Basically when I launch my FragmentAllNews  (containing a ViewPager) it's shown the second fragment/page instead of the first and when I swipe to the second I see the third... In other words It's always shown the next or the previous fragment/page depending on the swipe direction (left or right).</strong></p>
  <p>Sorry For my bad english :(</p>
  <p>here are my Fragments and FragmentStatePagerAdater class</p>
  <p>###1 FragmentAllNews which holds the viewpager and tablayout</p>
  <pre><code>public class FragmentAllNews extends Fragment {
      @Bind(R.id.tab_layout)
      TabLayout tabLayout;
      @Bind(R.id.all_news_viewpager)
      ViewPager viewPager;
  
      SessionManager sessionManager;
      String categoryId;
      String categoryName;
      ArrayList&lt;TabModel&gt; tabs;
  
  
      public static FragmentAllNews createNewInstance() {
          FragmentAllNews fragmentAllNews = new FragmentAllNews();
          return fragmentAllNews;
      }
  
      @Override
      public void onCreate(@Nullable Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          sessionManager = new SessionManager(getActivity());
          tabs = sessionManager.getSwitchedNewsValue() == 0 ?
                  StaticStorage.getTabData(0) :
                  StaticStorage.getTabData(1);
      }
  
  
      @Nullable
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          View fragmentView = inflater.inflate(R.layout.fragment_all_news, container, false);
          ButterKnife.bind(this, fragmentView);
          return fragmentView;
      }
  
      @Override
      public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
          super.onViewCreated(view, savedInstanceState);
  
          setViewPager(tabs);
          setTabLayout();
      }
  
      private void setTabLayout() {
          tabLayout.setupWithViewPager(viewPager);
          tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
              @Override
              public void onTabSelected(TabLayout.Tab tab) {
                  viewPager.setCurrentItem(tab.getPosition());
  
              }
  
              @Override
              public void onTabUnselected(TabLayout.Tab tab) {
  
              }
  
              @Override
              public void onTabReselected(TabLayout.Tab tab) {
  
              }
          });
      }
  
      private void setViewPager(ArrayList&lt;TabModel&gt; tabs) {
          ArrayList&lt;NewsFragmentModel&gt; frags = new ArrayList&lt;&gt;();
  
          for (int i = 0; i &lt; tabs.size(); i++) {
              if (i == 0) {
                  categoryId = tabs.get(i).cat_id;
                  categoryName = tabs.get(i).cat_name;
                  frags.add(new NewsFragmentModel(FragmentBreakingAndLatestNews.createNewInstance(categoryId, categoryName), categoryName));
              } else {
                  categoryId = tabs.get(i).cat_id;
                  categoryName = tabs.get(i).cat_name;
                  frags.add(new NewsFragmentModel(SwipableFragment.createNewInstance(categoryId, categoryName), categoryName));
              }
          }
  
          NewsPagerAdapter adapter = new NewsPagerAdapter(getChildFragmentManager(),frags);
          viewPager.setAdapter(adapter);
  
      }
  
      @Override
      public void onDestroyView() {
          super.onDestroyView();
          ButterKnife.unbind(this);
      }
  }
  </code></pre>
  <p>###2 SwipableFragment which the fragment attached to the viewpager</p>
  <pre><code>public class SwipableFragment extends Fragment implements NewsTitlesAdapter.RecyclerPositionListener {
      @Bind(R.id.recycler_view)
      RecyclerView recyclerView;
      @Bind(R.id.progess)
      ProgressBar progressBar;
      @Bind(R.id.cat_id)
      TextView categoryTextView;
  
      ArrayList&lt;NewsObj&gt; newsObjs;
      NewsTitlesAdapter newsTitlesAdapter;
      SessionManager sessionManager;
  
      private static String categoryId;
      private static String categoryName;
  
  
      public static SwipableFragment createNewInstance(String category_id, String category_name) {
  
          SwipableFragment swipableFragment = new SwipableFragment();
          Bundle box = new Bundle();
          box.putString(StaticStorage.NEWS_CATEGORY_ID, category_id);
          box.putString(StaticStorage.NEWS_CATEGORY, category_name);
          swipableFragment.setArguments(box);
          return swipableFragment;
      }
  
      @Override
      public void onCreate(@Nullable Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          sessionManager = new SessionManager(getActivity());
  
          categoryId = getArguments().getString(StaticStorage.NEWS_CATEGORY_ID);
          categoryName = getArguments().getString(StaticStorage.NEWS_CATEGORY);
  
  
          newsObjs = (sessionManager.getSwitchedNewsValue() == 0) ?
                  NewsData.getNewsRepublica(getActivity()) :
                  NewsData.getNewsNagarik(getActivity(),categoryName);
  
      }
  
      @Nullable
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          View view = inflater.inflate(R.layout.fragment_swipable, container, false);
          ButterKnife.bind(this, view);
          return view;
      }
  
      @Override
      public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
          super.onViewCreated(view, savedInstanceState);
  
          Log.i(&quot;category&quot;, categoryId + &quot; &quot; + categoryName);
          categoryTextView.setText(categoryId+&quot; &quot;+categoryName);
  
          LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
          recyclerView.setLayoutManager(linearLayoutManager);
          recyclerView.setHasFixedSize(true);
          recyclerView.setItemAnimator(new DefaultItemAnimator());
  
  
  
          newsTitlesAdapter = new NewsTitlesAdapter(newsObjs);
          newsTitlesAdapter.setOnRecyclerPositionListener(this);
          recyclerView.setAdapter(newsTitlesAdapter);
  
          recyclerView.addOnScrollListener(new EndlessScrollListener(linearLayoutManager) {
              @Override
              public void onLoadMore(int current_page) {
                  Log.i(&quot;categoryId&quot;, categoryId+&quot; &quot;+categoryName);
  
                  categoryTextView.setText(categoryId+&quot; &quot;+categoryName);
  
                  progressBar.setVisibility(View.VISIBLE);
  
                  new Handler().postDelayed(new Runnable() {
                      @Override
                      public void run() {
  
                          ArrayList&lt;NewsObj&gt; moreNews = sessionManager.getSwitchedNewsValue() == 0 ?
                                  NewsData.getNewsRepublica(getContext()) :
                                  NewsData.getNewsNagarik(getContext(),categoryName);
  
                          int curSize = newsTitlesAdapter.getItemCount();
                          newsObjs.addAll(moreNews);
                          newsTitlesAdapter.notifyItemRangeInserted(curSize, newsObjs.size() - 1);
                          progressBar.setVisibility(View.GONE);
                      }
                  }, 5000);
              }
          });
  
  
      }
  
  
      @Override
      public void onChildItemPositionListen(int position, View view) {
          if (view.getId() == R.id.news_share_text_view) {
  
          } else if (view.getId() == R.id.news_show_detail_text_view) {
  
          } else {
              Intent newsDetailIntent = new Intent(getActivity(), NewsDetailActivity.class);
              NewsObj newsObj = newsObjs.get(position);
              newsObj.setNewsCategory(categoryName);
  
              newsDetailIntent.putExtra(NewsDetailActivity.NEWS_TITLE_EXTRA_STRING, newsObj);
              startActivity(newsDetailIntent);
          }
      }
  }
  </code></pre>
  <p>###3 NewsPagerAdapter is the subclass extended from FragmentStatePagerAdapter</p>
  <pre><code>public class NewsPagerAdapter extends FragmentStatePagerAdapter {
      ArrayList&lt;NewsFragmentModel&gt; frags;
  
      public NewsPagerAdapter(FragmentManager fm) {
          super(fm);
      }
  
      public NewsPagerAdapter(FragmentManager fm,ArrayList&lt;NewsFragmentModel&gt; frags){
          super(fm);
          this.frags = frags;
      }
      @Override
      public Fragment getItem(int position) {
          return frags.get(position).getFrag();
      }
  
      @Override
      public int getCount() {
          return frags.size();
      }
  
      @Override
      public CharSequence getPageTitle(int position) {
  
          return frags.get(position).getTitle();
      }
  }
  </code></pre>
  <p>###4 NewsFragmentModel</p>
  <pre><code>public class NewsFragmentModel {
      private Fragment frag;
      private String title;
  
      public NewsFragmentModel(Fragment frag, String title) {
          this.frag = frag;
          this.title = title;
      }
  
      public Fragment getFrag() {
          return frag;
      }
  
      public void setFrag(Fragment frag) {
          this.frag = frag;
      }
  
      public String getTitle() {
          return title;
      }
  
      public void setTitle(String title) {
          this.title = title;
      }
  }
  </code></pre>
  <p>Am I doing any mistakes ? please help me.If you need more info please let me know. My final hope is Stackoverflow</p>