title: Viewpager fragments not shown after the first time
body: <p>I have an activity with 3 fragments (A, B, C). Fragment A consists of a <code>ViewPager</code> with 2 <code>ListFragments</code>. The user can tap on an item in any of the listfragments and by doing so, goes to fragment B.</p>
  
  <p>In fragment A I do:</p>
  
  <pre><code>@Override
  public void onAttach(Activity activity) {
      super.onAttach(activity);
      pagerAdapter = new PagerAdapter(getActivity().getSupportFragmentManager());
  }
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      View view = inflater.inflate(R.layout.fragmentA, container, false);
  
      vpPager = (ViewPager)view.findViewById(R.id.vpPager);
      vpPager.setOffscreenPageLimit(2);
      vpPager.setAdapter(pagerAdapter);
      vpPager.addOnPageChangeListener(this);
  
      return view;
  }
  </code></pre>
  
  <p>And the <code>PagerAdapter</code> is as follows:</p>
  
  <pre><code>private class PagerAdapter extends FragmentPagerAdapter {
      private final ListFragment1 lf1 = ListFragment1 .newInstance();
      private final ListFragment2 lf2 = ListFragment2 .newInstance();
  
      public PagerAdapter(android.support.v4.app.FragmentManager fm) {
          super(fm);
      }
  
      @Override
      public android.support.v4.app.Fragment getItem(int position) {
          switch (position) {
              case 0:  return lf1;
              case 1:  return lf2;
              default: return null;
          }
      }
  }
  </code></pre>
  
  <p>The first time the activity is shown, the viewpager list fragments are displayed correctly. </p>
  
  <p>The 2 viewpager fragments load data from a db, and I do this only once (when the fragments are created).</p>
  
  <p>The user can tap on an item and fragment B is displayed. If the user presses Back, fragment A is shown. However the list fragments are not shown (already an instance of them still exists).</p>
  
  <p>Could it be that the view has been destroyed, even though instances exist?</p>
  
  <p>What is wrong here? Is there a better approach?</p>
  
  <p><strong>EDIT</strong> </p>
  
  <p>If I use <code>newInstance</code> in the pager adapter, I get an <code>IllegalStateException: not attached to activity</code>. This is because I start an async task as follows:</p>
  
  <pre><code>@Override
  public void onPageSelected(int position) {
      Fragment fragment = pagerAdapter.getItem(position);
      if (fragment instanceof IPagedFragment) {
          ((IPagedFragment) fragment).onShown();
      }
  }
  </code></pre>
  
  <p>And <code>onShown</code> is:</p>
  
  <pre><code>@Override
  public void onShown() {
      myTask= new MyTask();
      myTask.execute((Void)null);
  }
  </code></pre>
  
  <p>When can I start the task so that I can be 100% sure that the fragment is attached to the activity and that the view has been created (I need to get listview, etc. from the layout).</p>