ViewPager data lost when coming back from next screen

After a little research, I found the solution to my problem.

I was originally calling getactivity().getSupportFragmentmanager(). However, the correct code is getChildFragmentManager()


In you adapter extend FragmentStatePagerAdapter instead of FragmentPagerAdapter it will solve your problem. Let me know if you have any problem.

Reason (quoting from javadoc)

  /**
   The [android.support.v4.view.PagerAdapter] that will provide
   fragments for each of the sections. We use a
   {@link FragmentPagerAdapter} derivative, which will keep every
   loaded fragment in memory. If this becomes too memory intensive, it
   may be best to switch to a
   [android.support.v4.app.FragmentStatePagerAdapter].
  */

public float getPageWidth(int position)
{
if (position == 0 || position == 2)
{
return 0.8f;
}
return 1f;
}

pager = (ViewPager) rootView.findViewById(R.id.pager);
pager.setOffscreenPageLimit(2);// no of fragments