How to prevent Fragment from loading data every time from server when swiping Tabs in TabLayout in android?
<p>I am a newbie with <code>Fragments</code>. I have 5 different <code>Fragments</code>in my <code>TabLayout</code>. In each fragment I am loading data from the server.</p>

<p>My problem is as follows:
When the first fragment loads it calls the function and loads data from the server and displays on the <code>RecyclerView</code>. </p>

<p>Then When I swipe to the second tab the server data loads and so on. But when I swipe again to say fragment one the data is loading again from the server and the <code>RecyclerView</code> needs to be set again.</p>

<p>Is there anyway where I can load the server data only once (without saving it to a local database) and set the <code>recyclerview</code> only once so that no matter how many times I swipe the tab the data does not load again? I have referred <a href="https://stackoverflow.com/questions/25337570/tabs-fragment-fetch-data-every-time-i-change-tab">here</a> but i really didnt understand how to solve my problem.I am posting a snippet of my code.</p>

<p>TabActivity</p>

<pre><code>public class TabActivity extends AppCompatActivity {
ViewPager viewpager;
Toolbar toolbar;
TabLayout tablayout;
ViewPagerAdapter viewPagerAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tab_activity);
    initialize();




}

private void initialize() {
    viewpager = (ViewPager) findViewById(R.id.viewpager);
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    tablayout = (TabLayout) findViewById(R.id.tablayout);
    setSupportActionBar(toolbar);
    viewPagerAdapter=new ViewPagerAdapter(getSupportFragmentManager());
    viewpager.setAdapter(viewPagerAdapter);
    tablayout.setTabsFromPagerAdapter(viewPagerAdapter);
    tablayout.setupWithViewPager(viewpager);

}


}

 class ViewPagerAdapter extends FragmentStatePagerAdapter implements ViewPager.OnPageChangeListener {

public ViewPagerAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int position) {
    switch (position) {
        case 0:return LocatedEvents.newInstance();

        case 1:return QrCodeScanner.newInstance();
        case 2:return NewsFeed.newInstance();

        case 3:return CreatePoll.newInstance();
        case 4:return MyProfile.newInstance();
    }
    return NewsFeed.newInstance();
}

@Override
public int getCount() {
    return 5;
}
@Override
public CharSequence getPageTitle(int position) {
    return "Tab " + (position + 1);
}

@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

}

@Override
public void onPageSelected(int position) {

}

@Override
public void onPageScrollStateChanged(int state) {

}
}
</code></pre>

<p>NewsFeed Fragment</p>

<pre><code>public class NewsFeed extends Fragment {
@Nullable

public static NewsFeed newInstance() {
    return new NewsFeed();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.newsfeed_fragment, container, false);
    load_data();
    return view;
}
}
</code></pre>