title: Android - Saving state of Viewpager inside a fragment
body: <p>I have a container fragment <code>Acadmey</code> which is containing a <code>viewpager</code> and <code>viewpager</code> consist of different <code>child fragments</code>. In the child fragments of View pager I invoke the json request to get the data to be populated. For the first time every thing works fine but when I get back to <code>main activity</code> and again get backs to the view pager all the content of the <code>child fragment</code> is <strong><em>wiped out</em></strong> and <code>onActivityCreated</code> calls only of the first time therefore my viewpagers pages are <strong><em>blank</em></strong> for every second or n times I opened . How can I save the content of the child fragment for reuse or is their any method which will defentaly been <code>callbacked</code> every time when viewpager's every child fragment reopens.</p>
  
  <p><em>My Container Fragment :-</em></p>
  
  <pre><code>  public class Academy extends Fragment {
    ViewPager viewPager = null;
    TabLayout tabLayout;
    MyViewPagerAdapter adapter;
    private String myString = "hello";
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  
  
  }
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      Log.d("xx", "view started .............. papa");
      return inflater.inflate(R.layout.academy, container, false);
  }
  
  @Override
  public void onActivityCreated( Bundle savedInstanceState) {
      super.onActivityCreated(savedInstanceState);
      tabLayout = (TabLayout) getActivity().findViewById(R.id.tablayoutacademy);
      viewPager = (ViewPager) getActivity().findViewById(R.id.academypager);
      adapter = new MyViewPagerAdapter(getActivity().getSupportFragmentManager());
      adapter.addfragment(new FragmentCourse(), "COURSES");
      adapter.addfragment(new FragmentEducator(), "EDUCATORS");
      viewPager.setAdapter(adapter);
      tabLayout.setupWithViewPager(viewPager);
  
  }
  
  }
  </code></pre>
  
  <p>My Viewpager Child Fragment(Courses)</p>
  
  <pre><code> @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  
  
  }
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      Log.d("xx", "view course started...........................");
      progress=new ProgressDialog(getContext());
      progress.setMessage("Nukeing..");
      progress.show();
      return inflater.inflate(R.layout.fragmentcourse, container, false);
  
  }
  
  @Override
  public void onActivityCreated(Bundle jhoola2) {
      super.onActivityCreated(jhoola2);
            Log.d("xx", "acti  cource started");
      recyclerView = (RecyclerView) getActivity().findViewById(R.id.rv2);
      manager = new LinearLayoutManager(getActivity());
      recyclerView.setLayoutManager(manager);
      recyclerView.setHasFixedSize(true);
      Handler handler = new Handler() {
  
  
          @Override
          public void handleMessage(Message msg) {
              super.handleMessage(msg);
              MyImageCompress ic=new MyImageCompress();
  
              Bundle bundle=new Bundle();
              bundle=msg.getData();
  
              title1 = bundle.getStringArrayList("C_name");
              dec1=bundle.getStringArrayList("Discription");
              image1=bundle.getStringArrayList("Image");
              for (int i = 0; i &lt; image1.size(); i++) {
  
                  imageArray1.add(ic.StringToBitMap(image1.get(i)));
                  Log.d( "handleMessage: "," Final parse" +i);
              }
              Log.d("handleMessage: EDU ", title1.size() + "" + dec1.size() + imageArray1.size());
                                  adapter = new RecyclerAdaptra(title1, dec1, imageArray1);
              progress.hide();
  
              recyclerView.setAdapter(adapter);
              adapter.notifyDataSetChanged();
  
          }
      };
     GetCources gc= new GetCources(url,handler);
      gc.executeRequest();
      }
  }
  </code></pre>
  
  <p>*My ViewPager Adapter *</p>
  
  <pre><code>class MyViewPagerAdapter extends FragmentPagerAdapter {
  ArrayList&lt;Fragment&gt; fragments = new ArrayList&lt;Fragment&gt;();
  ArrayList&lt;String&gt; tabtitles = new ArrayList&lt;String&gt;();
  
  public void addfragment(Fragment fragments, String titles) {
      this.fragments.add(fragments);
      this.tabtitles.add(titles);
  }
  
  public MyViewPagerAdapter(FragmentManager fm) {
      super(fm);
  }
  
  @Override
  public Fragment getItem(int i) {
      return fragments.get(i);
  
  }
  
  @Override
  public int getCount() {
      return fragments.size();
  }
  
  @Override
  public CharSequence getPageTitle(int position) {
      return tabtitles.get(position);
  }
  }
  </code></pre>