title: Fragment and null getActivity after onAttach
body: <p>I'm trying to understand under what circumstances can getActivity() return null in a fragment AFTER onAttach. I typically start an async task in onCreate or onCreateView inside my fragments but I'm getting error reports indicating sometimes getActivity() is null when the async task finishes. Error reports are coming in via crashlytics but can't reproduce them.</p>
  
  <p>The async tasks are "blocking" - I display a modal non-dismissable progress bar. Also rotation is prevented by calling setRequestedOrientation.</p>
  
  <p>I'm using v4 support Fragment and FragmentActivity. Fragments are set to retain state.</p>
  
  <p>What am I missing? Are there other config changes that may cause the fragment to be detached?</p>
  
  <p>I tried temporarily enabling rotation and the dev option to destroy activity after leaving it but still can't reproduce this...</p>
  
  <p>Here's some of the relevant code inside one of my fragments, in this case it would sometimes break with an NPE at <code>activity.dismissSpinner</code>:</p>
  
  <pre><code>@Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      checkIfLoggedIn();
  }
  
  public void checkIfLoggedIn() {
      LoginActivity activity = (LoginActivity)getActivity();
      activity.showSpinner("Connecting, please wait...");
  
      AsyncTask&lt;String, Void, JsonResponse&gt; asyncTask = new AsyncTask&lt;String, Void, JsonResponse&gt;() {
          protected JsonResponse doInBackground(String... notused) {
              return cmsServer().getCurrentUser(getActivity());
          }
  
          protected void onPostExecute(JsonResponse result) {               
              LoginActivity activity = (LoginActivity)getActivity();
              activity.dismissSpinner();
              //...more stuff here
          }
      };
      asyncTask.execute();
  }
  </code></pre>