title: how to stop ViewPager fragment auto Initialize
body: <p>I have 3 fragments; when I start the app all the fragments are getting data from the server at the same time, but I need something like that <img src="https://i.stack.imgur.com/QwHUN.png" alt="youtube view pager fragment  "></p>
  
  <p>When the user slides the tab then the fragment starts downloading data from the server,</p>
  
  <p>in my case, all fragments start asynctask at the same time, so if you have slow internet then the app is going down   </p>
  
  <pre><code>public class HeadLineFragment extends Fragment {
  
  private static final String TAG = "RecyclerViewFragment";
  private static final String KEY_LAYOUT_MANAGER = "layoutManager";
  private static final int SPAN_COUNT = 2;
  private static final int DATASET_COUNT = 60;
  private static final String DW_HOME_PAGE_URL = "https://rss.dw.de/xml/rss-urd-all";
  protected LayoutManagerType mCurrentLayoutManagerType;
  protected RadioButton mLinearLayoutRadioButton;
  protected RadioButton mGridLayoutRadioButton;
  protected RecyclerView mRecyclerView;
  protected RecyclerAdapter mAdapter;
  protected RecyclerView.LayoutManager mLayoutManager;
  
  protected ArrayList&lt;String&gt; titles = new ArrayList();
  protected ArrayList&lt;String&gt; ImageURLS = new ArrayList&lt;&gt;();
  protected ArrayList&lt;String&gt; description = new ArrayList();
  protected ArrayList&lt;RssItem&gt; mDataset = new ArrayList&lt;&gt;();
  private TextView textView;
  private Context context;
  private Elements metalinks;
  private String mImageURL;
  private Document documentImage;
  private ProgressBar mProgressBar;
  private int mItemNumber;
  private myPagerAdupter _myPagerAdupter;
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
  
  
      super.onCreate(savedInstanceState);
  
  
  
      setRetainInstance(true);
      this._myPagerAdupter= new myPagerAdupter(getChildFragmentManager(),getContext());
  
  
  
  }
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
      View rootView = inflater.inflate(R.layout.head_line_fragment, container, false);
  
  
      new LoadRssFeedsItems().execute(DW_HOME_PAGE_URL);
      this.context = rootView.getContext();
  
  
  
      mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);
      mProgressBar = (ProgressBar) rootView.findViewById(R.id.Progressbar);
      mProgressBar.setProgress(0);
  
      LoadRssFeedsItems loadRssFeedsItems = new LoadRssFeedsItems();
  
      loadRssFeedsItems.setCircularProgressView(mProgressBar);
  
  
  
  
      mLayoutManager = new LinearLayoutManager(getActivity());
  
      mCurrentLayoutManagerType = LayoutManagerType.LINEAR_LAYOUT_MANAGER;
  
      if (savedInstanceState != null) {
  
          mCurrentLayoutManagerType = (LayoutManagerType) savedInstanceState
                  .getSerializable(KEY_LAYOUT_MANAGER);
      }
      setRecyclerViewLayoutManager(mCurrentLayoutManagerType);
  
  
      return rootView;
  }
  
  
  public void setRecyclerViewLayoutManager(LayoutManagerType layoutManagerType) {
      int scrollPosition = 0;
  
      if (mRecyclerView.getLayoutManager() != null) {
          scrollPosition = ((LinearLayoutManager) mRecyclerView.getLayoutManager())
                  .findFirstCompletelyVisibleItemPosition();
      }
  
      switch (layoutManagerType) {
          case GRID_LAYOUT_MANAGER:
              mLayoutManager = new GridLayoutManager(getActivity(), SPAN_COUNT);
              mCurrentLayoutManagerType = LayoutManagerType.GRID_LAYOUT_MANAGER;
              break;
          case LINEAR_LAYOUT_MANAGER:
              mLayoutManager = new LinearLayoutManager(getActivity());
              mCurrentLayoutManagerType = LayoutManagerType.LINEAR_LAYOUT_MANAGER;
              break;
          default:
              mLayoutManager = new LinearLayoutManager(getActivity());
              mCurrentLayoutManagerType = LayoutManagerType.LINEAR_LAYOUT_MANAGER;
      }
  
      mRecyclerView.setLayoutManager(mLayoutManager);
      mRecyclerView.scrollToPosition(scrollPosition);
  }
  
  @Override
  public void onSaveInstanceState(Bundle savedInstanceState) {
  
      savedInstanceState.putSerializable(KEY_LAYOUT_MANAGER, mCurrentLayoutManagerType);
      super.onSaveInstanceState(savedInstanceState);
  }
  
  
  private enum LayoutManagerType {
      GRID_LAYOUT_MANAGER,
      LINEAR_LAYOUT_MANAGER
  }
  
  
  private class LoadRssFeedsItems extends AsyncTask&lt;String, Integer, String&gt; {
      private String mTitle, mDescription, mLink, mPubDate;
  
      private ProgressBar circularProgressView;
  
      public void setCircularProgressView(ProgressBar circularProgressView) {
          this.circularProgressView = circularProgressView;
      }
  
  
      protected String doInBackground(String... urls) {
          try {
  
              Document rssDocument = Jsoup.connect(DW_HOME_PAGE_URL).timeout(Constants.TIME_OUT).ignoreContentType(true).parser(Parser.xmlParser()).get();
  
  
              Elements mItems = rssDocument.select(Constants.ITEM);
              RssItem rssItem;
              mItemNumber = 0;
              for (Element element : mItems) {
  
                  mItemNumber++;
                  mTitle = element.select("title").first().text();
                  mDescription = element.select("description").first().text();
                  mLink = element.select("link").first().text();
                  mPubDate = element.select("pubDate").first().text();
  
  
                  Log.i(TAG, "Item title: " + (mTitle == null ? "N/A" : mTitle));
                  Log.i(TAG, "Item Description: " + (mDescription == null ? "N/A" : mDescription));
                  Log.i(TAG, "Item link: " + (mLink == null ? "N/A" : mLink));
                  Log.i(TAG, "Item data: " + (mPubDate == null ? "N/A" : mPubDate));
                  Log.i(TAG, "Item image link: " + (mImageURL == null ? "N/A" : mImageURL));
                  Log.i(TAG, "item: : " + mItemNumber);
                  rssItem = new RssItem(mTitle, mDescription, mPubDate, mLink, mItemNumber, getContext());
                  mDataset.add(rssItem);
  
  
              }
  
          } catch (Exception e) {
              e.printStackTrace();
          }
          return "d";
      }
  
      @Override
      protected void onProgressUpdate(Integer... value) {
          super.onProgressUpdate(value);
          if (circularProgressView != null) {
  
              circularProgressView.setProgress(value[0]);
          }
  
      }
  
  
      protected void onPostExecute(String result) {
          if (mDataset.isEmpty()) {
              Snackbar mSnackbar = Snackbar.make(getView(), "Unable to connect  Home page", Snackbar.LENGTH_LONG)
                      .setAction("RETRY", new View.OnClickListener() {
                          @Override
                          public void onClick(View view) {
  
                              new LoadRssFeedsItems().execute(Constants.HALAT_HAZRA);
  
  
                          }
                      });
              mSnackbar.show();
          } else {
              mAdapter = new RecyclerAdapter(mDataset);
              Log.i(TAG, "m data set size in current affairs: " + mDataset.size());
  
              mRecyclerView.setAdapter(mAdapter);
              getImageUrls get_ImageUrls = new getImageUrls();
  
              get_ImageUrls.execute();
          }
      }
  }
  
  private class getImageUrls extends AsyncTask&lt;Void, Void, Void&gt; {
      String mLink;
      String mLastHalfUrl;
      String url;
      int dash_A_index;
  
      @Override
      protected void onPreExecute() {
          Snackbar.make(getView(), "Loding Photos...", Snackbar.LENGTH_LONG)
                  .setAction("Action", null).show();
      }
  
      @Override
      protected Void doInBackground(Void... params) {
  
          Log.i(TAG, "Item image link in seacond thread: " + (mImageURL == null ? "N/A" : mImageURL));
  
          try {
  
  
  
              for (int i = 0; i &lt; mDataset.size(); i++) {
                  Thread.sleep(50);
                  mLink = mDataset.get(i).getLink();
  
                  dash_A_index = mLink.indexOf(Constants.BACK_SLASH_A);
                  mLastHalfUrl = mLink.substring(dash_A_index);
                  url = mLink.replace(Constants.BASE_URL, "");
                  url = url.replace(mLastHalfUrl, "");
                  url = StringUtils.replaceEach(URLEncoder.encode(url, "UTF-8"), new String[]{"+", "*", "%7E"}, new String[]{"%20", "%2A", "~"});
                  StringBuilder sb = new StringBuilder(url);
                  url = sb.append(mLastHalfUrl).toString();
                  url = Constants.BASE_URL + url;
                  documentImage = Jsoup.connect(url).timeout(Constants.TIME_OUT).get();
                  metalinks = documentImage.select(Constants.MATA_PROPTY_IMAGE);
                  mImageURL = metalinks.attr(Constants.CONTENT);
                  Log.i(TAG, "Item image link in seacond thread home page: " + (mImageURL == null ? "N/A" : mImageURL));
                  mDataset.get(i).setImageUrl(mImageURL);
  
              }
  
          } catch (Exception e) {
  
          }
          return null;
      }
  
      @Override
      protected void onPostExecute(Void aVoid) {
  
          Constants.IS_GET_IMAGE_URL=true;
          mAdapter.notifyDataSetChanged();
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity
  implements NavigationView.OnNavigationItemSelectedListener, MaterialTabListener {
  
      private Bitmap bitmap;
      RecyclerView recyclerView;
      private static final String TAG = "viewPge";
      RecyclerView.LayoutManager layoutManager;
      RecyclerView.Adapter RecyclerViewadapter;
      MaterialTabHost tabHost;
      private String[] tabs;
  
      ViewPager mViewPager;
      private FragmentManager fragmentManager;
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
          setSupportActionBar(toolbar);
          getSupportActionBar().setElevation(0);
  
  
          tabs = getResources().getStringArray(R.array.tabs);
  
          tabHost = (MaterialTabHost) this.findViewById(R.id.materialTabHost);
          mViewPager = (ViewPager) this.findViewById(R.id.myViewPager);
  
  
          this.mViewPager.setAdapter(new myPagerAdupter(getSupportFragmentManager(),getApplicationContext()));
  
          this.mViewPager.setOffscreenPageLimit(2);
          this.mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
              @Override
              public void onPageSelected(int position) {
  
                  tabHost.setSelectedNavigationItem(position);
              }
          });
  
  
          for (int i = 0; i &lt; 3; i++) {
              tabHost.addTab(
                      tabHost.newTab().setText(tabs[i])
  
  
                              .setTabListener(this)
              );
          }
  
  
          FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
          fab.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                          .setAction("Action", null).show();
  
  
              }
          });
  
          DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
          ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                  this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
          drawer.setDrawerListener(toggle);
          toggle.syncState();
  
          NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
          navigationView.setNavigationItemSelectedListener(this);
      }
          @Override
      public void onBackPressed() {
          DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
          if (drawer.isDrawerOpen(GravityCompat.START)) {
              drawer.closeDrawer(GravityCompat.START);
          } else {
              super.onBackPressed();
          }
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.main, menu);
          return true;
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          // Handle action bar item clicks here. The action bar will
          // automatically handle clicks on the Home/Up button, so long
          // as you specify a parent activity in AndroidManifest.xml.
          int id = item.getItemId();
  
          //noinspection SimplifiableIfStatement
          if (id == R.id.action_settings) {
              return true;
          }
  
          return super.onOptionsItemSelected(item);
      }
  
      @SuppressWarnings("StatementWithEmptyBody")
      @Override
      public boolean onNavigationItemSelected(MenuItem item) {
          // Handle navigation view item clicks here.
          int id = item.getItemId();
  
          if (id == R.id.nav_camera) {
              // Handle the camera action
          } else if (id == R.id.nav_gallery) {
  
          } else if (id == R.id.nav_slideshow) {
  
          } else if (id == R.id.nav_manage) {
  
          } else if (id == R.id.nav_share) {
  
          } else if (id == R.id.nav_send) {
  
          }
  
          DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
          drawer.closeDrawer(GravityCompat.START);
          return true;
      }
  
      @Override
      public void onTabSelected(MaterialTab tab) {
  
  
  
          mViewPager.setCurrentItem(tab.getPosition());
  
      }
  
      @Override
      public void onTabReselected(MaterialTab tab) {
  
      }
  
      @Override
      public void onTabUnselected(MaterialTab tab) {
  
      }
  
  
      public void addFragment() {
          fragmentManager = getSupportFragmentManager();
          android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
          healthNewFragment recylerViewFragment = new healthNewFragment();
          fragmentTransaction.replace(R.id.frag_contaner, recylerViewFragment, "TAG_FRAGEMENT");
          //  fragmentTransaction.addToBackStack(null);
          fragmentTransaction.commit();
      }
     }
  </code></pre>
  
  <p><strong>Fragment 2</strong></p>
  
  <pre><code>    @Override
  public void onCreate(Bundle savedInstanceState) {
  
  
      super.onCreate(savedInstanceState);
  
  
      this.context = getContext();
      Log.i(TAG_LIFE, "Inside onCreat");
  
  
      new LoadRssFeedsItems().execute(DW_HOME_PAGE_URL);
  
  
  }
  
  @Override
  public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
      super.onViewCreated(view, savedInstanceState);
      Log.i(TAG_LIFE, "Inside onViewCreated");
  }
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
      View rootView = inflater.inflate(R.layout.head_line_fragment, container, false);
  
      this.context = rootView.getContext();
  
  
      Log.i(TAG_LIFE, "Inside onCreateView");
  
      mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);
  
      mLayoutManager = new LinearLayoutManager(getActivity());
  
      mCurrentLayoutManagerType = LayoutManagerType.LINEAR_LAYOUT_MANAGER;
  
      if (savedInstanceState != null) {
  
          mCurrentLayoutManagerType = (LayoutManagerType) savedInstanceState
                  .getSerializable(KEY_LAYOUT_MANAGER);
      }
      setRecyclerViewLayoutManager(mCurrentLayoutManagerType);
  
  
      return rootView;
  }
  
  
  public void setRecyclerViewLayoutManager(LayoutManagerType layoutManagerType) {
      int scrollPosition = 0;
  
  
      if (mRecyclerView.getLayoutManager() != null) {
          scrollPosition = ((LinearLayoutManager) mRecyclerView.getLayoutManager())
                  .findFirstCompletelyVisibleItemPosition();
      }
  
      switch (layoutManagerType) {
          case GRID_LAYOUT_MANAGER:
              mLayoutManager = new GridLayoutManager(getActivity(), SPAN_COUNT);
              mCurrentLayoutManagerType = LayoutManagerType.GRID_LAYOUT_MANAGER;
              break;
          case LINEAR_LAYOUT_MANAGER:
              mLayoutManager = new LinearLayoutManager(getActivity());
              mCurrentLayoutManagerType = LayoutManagerType.LINEAR_LAYOUT_MANAGER;
              break;
          default:
              mLayoutManager = new LinearLayoutManager(getActivity());
              mCurrentLayoutManagerType = LayoutManagerType.LINEAR_LAYOUT_MANAGER;
      }
  
      mRecyclerView.setLayoutManager(mLayoutManager);
      mRecyclerView.scrollToPosition(scrollPosition);
  }
  
  @Override
  public void onSaveInstanceState(Bundle savedInstanceState) {
  
      savedInstanceState.putSerializable(KEY_LAYOUT_MANAGER, mCurrentLayoutManagerType);
      super.onSaveInstanceState(savedInstanceState);
  }
  
  
  private enum LayoutManagerType {
      GRID_LAYOUT_MANAGER,
      LINEAR_LAYOUT_MANAGER
  }
  
  
  
  private class LoadRssFeedsItems extends AsyncTask&lt;String, Void, String&gt; {
      private String mTitle, mDescription, mLink, mPubDate;
  
      protected String doInBackground(String... urls) {
          try {
  
              Document rssDocument = Jsoup.connect(Constants.HEALTH).timeout(Constants.TIME_OUT).ignoreContentType(true).parser(Parser.xmlParser()).get();
  
              Document imageURLDocument;
  
  
              Elements mItems = rssDocument.select(Constants.ITEM);
              RssItem rssItem;
              String mLastHalfUrl;
              String url;
              int dash_A_index;
              int i = 0;
              for (Element element : mItems) {
                  i++;
                  mTitle = element.select("title").first().text();
                  mDescription = element.select("description").first().text();
                  mLink = element.select("link").first().text();
                  mPubDate = element.select("pubDate").first().text();
                  Log.i(TAG, "Item title: " + (mTitle == null ? "N/A" : mTitle));
                  Log.i(TAG, "Item Description: " + (mDescription == null ? "N/A" : mDescription));
                  Log.i(TAG, "Item link: " + (mLink == null ? "N/A" : mLink));
                  Log.i(TAG, "Item data: " + (mPubDate == null ? "N/A" : mPubDate));
                  Log.i(TAG, "Item image link: " + (mImageURL == null ? "N/A" : mImageURL));
                  Log.i(TAG, "int i : " + i);
                  rssItem = new RssItem(mTitle, mDescription, mPubDate, mLink, i, getContext());
                  mDataset.add(rssItem);
  
              }
  
  
  
          } catch (Exception e) {
              e.printStackTrace();
          }
          return "d";
      }
  
  
      protected void onPostExecute(String result) {
  
          if (mDataset == null) {
              Log.i(TAG, "Connecction time out");
              Toast.makeText(getActivity(), "Connecction time ou)",
                      Toast.LENGTH_SHORT).show();
          }
          if (mDataset.isEmpty()) {
              Snackbar mSnackbar = Snackbar.make(getView(), "Unable to connect Health", Snackbar.LENGTH_LONG)
                      .setAction("RETRY", new View.OnClickListener() {
                          @Override
                          public void onClick(View view) {
  
                              new LoadRssFeedsItems().execute(Constants.HALAT_HAZRA);
  
  
                          }
                      });
              mSnackbar.show();
          } else {
              mAdapter = new RecyclerAdapter(mDataset);
              Log.i(TAG, "m data set size: " + mDataset.size());
  
              mRecyclerView.setAdapter(mAdapter);
              getImageUrls get_ImageUrls = new getImageUrls();
              get_ImageUrls.execute();
          }
      }
  }
  
  private class getImageUrls extends AsyncTask&lt;Void, Void, Void&gt; {
      String mLink;
      String mLastHalfUrl;
      String url;
      int dash_A_index;
  
      @Override
      protected Void doInBackground(Void... params) {
  
          Log.i(TAG, "Item image link in seacond thread: " + (mImageURL == null ? "N/A" : mImageURL));
  
  
          try {
  
  
              for (int i = 0; i &lt; mDataset.size(); i++) {
                  mLink = mDataset.get(i).getLink();
                  dash_A_index = mLink.indexOf(Constants.BACK_SLASH_A);
                  mLastHalfUrl = mLink.substring(dash_A_index);
                  url = mLink.replace(Constants.BASE_URL, "");
                  url = url.replace(mLastHalfUrl, "");
                  url = StringUtils.replaceEach(URLEncoder.encode(url, "UTF-8"), new String[]{"+", "*", "%7E"}, new String[]{"%20", "%2A", "~"});
                  StringBuilder sb = new StringBuilder(url);
                  url = sb.append(mLastHalfUrl).toString();
                  url = Constants.BASE_URL + url;
                  documentImage = Jsoup.connect(url).get();
                  metalinks = documentImage.select(Constants.MATA_PROPTY_IMAGE);
                  mImageURL = metalinks.attr(Constants.CONTENT);
                  Log.i(TAG, "Item image link in seacond thread: " + (mImageURL == null ? "N/A" : mImageURL));
                  mDataset.get(i).setImageUrl(mImageURL);
  
              }
  
          } catch (Exception e) {
  
          }
          return null;
      }
  
      @Override
      protected void onPostExecute(Void aVoid) {
  
          mAdapter.notifyDataSetChanged();
      }
  }
  </code></pre>