title: android compatibility package - fragment ... not attached to Activity
body: <hr>
  
  <p>Hello,
  I'm facing this problem :
  I'm using Compatibility package to use fragment in android application (min SDK 2.1).</p>
  
  <p>An random exception occurs on fragment sometimes and I can't figure out why.
  This is the stack trace I receive:</p>
  
  <pre><code>java.lang.IllegalStateException: Fragment FeedListFragment{438a54e8} not attached to Activity
      at android.support.v4.app.Fragment.getLoaderManager(Fragment.java:715)
      at com.myappli.ui.FeedListFragment.refreshUpdateDate(FeedListFragment.java:283)
      at com.myappli.ui.phone.FeedListActivity.onReceiveResult(FeedListActivity.java:277)
      at com.myappli.data.rssplayer.service.KTDetachableResultReceiver.onReceiveResult(KTDetachableResultReceiver.java:55)
      at android.os.ResultReceiver$MyRunnable.run(ResultReceiver.java:43)
      at android.os.Handler.handleCallback(Handler.java:587)
      at android.os.Handler.dispatchMessage(Handler.java:92)
      at android.os.Looper.loop(Looper.java:136)
      at android.app.ActivityThread.main(ActivityThread.java:4425)
      at java.lang.reflect.Method.invokeNative(Native Method)
      at java.lang.reflect.Method.invoke(Method.java:521)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
      at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>Here is the corresponding code I'm calling in the Fragment class:</p>
  
  <pre><code>public class FeedListFragment extends ListFragment implements LoaderManager.LoaderCallbacks&lt;Cursor&gt;, FeedListCursorAdapterListener {
  
  ...
      public void refreshUpdateDate() {
      getLoaderManager().restartLoader(LAST_UPDATE_CURSOR_ID, null, this);
      }
  ...
  }
  </code></pre>
  
  <p>Here is the code of the activity that calls the fragment:</p>
  
  <pre><code>private FeedListFragment mCursorLoaderListFg;
  
  if (!isFinishing()) {
      mCursorLoaderListFg.refreshUpdateDate();
      mCursorLoaderListFg.refreshDisplay();
      mCursorLoaderListFg.hideLoadingArticles();
  }
  </code></pre>
  
  <p>Here is the fragment source code for getLoaderManager():</p>
  
  <pre><code>/**
  * Return the LoaderManager for this fragment, creating it if needed.
  */
  public LoaderManager getLoaderManager() {
      if (mLoaderManager != null) {
          return mLoaderManager;
      }
      if (mActivity == null) {
          throw new IllegalStateException("Fragment " + this + " not attached to Activity");
      }
      mCheckedForLoaderManager = true;
      mLoaderManager = mActivity.getLoaderManager(mIndex, mLoadersStarted, true);
      return mLoaderManager;
  }
  </code></pre>
  
  <p>Here are the steps of the application before crash:</p>
  
  <ol>
  <li>The activity calls methods in background.</li>
  <li>Callback methods are called on the activity</li>
  <li>In this callback method, the activity calls the fragment</li>
  <li>The fragment tries to launch some methods (like getLoadManager()) that need activity.</li>
  <li>The fragment  is not attached anymore to the activity => crash</li>
  </ol>
  
  <p>We tried two fixes:</p>
  
  <ul>
  <li>test on activity to check if the activity is finishing (like the code above)</li>
  <li>test on the fragment with the method isDetached.</li>
  </ul>
  
  <p>Thanks for your help!!!</p>
  
  <p>Do not hesitate to ask me more information if I'm not clear.</p>