title: When is fragment finally attached to activity?
body: <p>I have a main fragment with a viewpager inside it. This viewpager has 2 pages (list fragments). When I start the activty, the main fragment is shown and I also show the first paged fragment. This paged fragment displays data from a db using <code>AsyncTask</code>.</p>
  
  <p>In the main fragment I have:</p>
  
  <pre><code>@Override
  public void onViewCreated(View view, Bundle savedInstanceState) {
      super.onViewCreated(view, savedInstanceState);
      onPageSelected(0);
  }
  
  @Override
  public void onPageSelected(int position) {
      Fragment fragment = (Fragment) pagerAdapter.instantiateItem(viewPager, position);
      if (fragment instanceof IPagedFragment) {
          ((IPagedFragment) fragment).onShown(getActivity());
      }
  }
  </code></pre>
  
  <p>And the interface is:</p>
  
  <pre><code>public interface IPagedFragment {
      void onShown(FragmentActivity activity);
  }
  </code></pre>
  
  <p>The first issue I have is that I have to pass the activity as a parameter because when <code>onShown</code> gets called, the activity is still null.</p>
  
  <p>Furthermore, the paged fragments use progressbar logic similar to the LoginActivity sample. I also get the following exception:</p>
  
  <blockquote>
    <p>IllegalStateException: Fragment PagedFragment1{4201f758} not attached to Activity 
    at android.support.v4.app.Fragment.getResources(Fragment.java:620)</p>
  </blockquote>
  
  <p>So what is the correct stage to start retrieving data from db once the paged fragment is fully available to the UI?</p>