title: Fragment MyFragment not attached to Activity
body: <p>I've created a small test app which represents my problem.
  I'm using ActionBarSherlock to implement tabs with (Sherlock)Fragments.</p>
  
  <p>My code:
  <code>TestActivity.java</code></p>
  
  <pre><code>public class TestActivity extends SherlockFragmentActivity {
      private ActionBar actionBar;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setupTabs(savedInstanceState);
      }
  
      private void setupTabs(Bundle savedInstanceState) {
          actionBar = getSupportActionBar();
          actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
  
          addTab1();
          addTab2();
      }
  
      private void addTab1() {
          Tab tab1 = actionBar.newTab();
          tab1.setTag("1");
          String tabText = "1";
          tab1.setText(tabText);
          tab1.setTabListener(new TabListener&lt;MyFragment&gt;(TestActivity.this, "1", MyFragment.class));
  
          actionBar.addTab(tab1);
      }
  
      private void addTab2() {
          Tab tab1 = actionBar.newTab();
          tab1.setTag("2");
          String tabText = "2";
          tab1.setText(tabText);
          tab1.setTabListener(new TabListener&lt;MyFragment&gt;(TestActivity.this, "2", MyFragment.class));
  
          actionBar.addTab(tab1);
      }
  }
  </code></pre>
  
  <p><code>TabListener.java</code></p>
  
  <pre><code>public class TabListener&lt;T extends SherlockFragment&gt; implements com.actionbarsherlock.app.ActionBar.TabListener {
      private final SherlockFragmentActivity mActivity;
      private final String mTag;
      private final Class&lt;T&gt; mClass;
  
      public TabListener(SherlockFragmentActivity activity, String tag, Class&lt;T&gt; clz) {
          mActivity = activity;
          mTag = tag;
          mClass = clz;
      }
  
      /* The following are each of the ActionBar.TabListener callbacks */
  
      public void onTabSelected(Tab tab, FragmentTransaction ft) {
          SherlockFragment preInitializedFragment = (SherlockFragment) mActivity.getSupportFragmentManager().findFragmentByTag(mTag);
  
          // Check if the fragment is already initialized
          if (preInitializedFragment == null) {
              // If not, instantiate and add it to the activity
              SherlockFragment mFragment = (SherlockFragment) SherlockFragment.instantiate(mActivity, mClass.getName());
              ft.add(android.R.id.content, mFragment, mTag);
          } else {
              ft.attach(preInitializedFragment);
          }
      }
  
      public void onTabUnselected(Tab tab, FragmentTransaction ft) {
          SherlockFragment preInitializedFragment = (SherlockFragment) mActivity.getSupportFragmentManager().findFragmentByTag(mTag);
  
          if (preInitializedFragment != null) {
              // Detach the fragment, because another one is being attached
              ft.detach(preInitializedFragment);
          }
      }
  
      public void onTabReselected(Tab tab, FragmentTransaction ft) {
          // User selected the already selected tab. Usually do nothing.
      }
  }
  </code></pre>
  
  <p><code>MyFragment.java</code></p>
  
  <pre><code>public class MyFragment extends SherlockFragment {
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          new AsyncTask&lt;Void, Void, Void&gt;() {
  
              @Override
              protected Void doInBackground(Void... params) {
                  try {
                      Thread.sleep(2000);
                  } catch (InterruptedException ex) {
                  }
                  return null;
              }
  
              @Override
              protected void onPostExecute(Void result){
                  getResources().getString(R.string.app_name);
              }
  
          }.execute();
      }
  }
  </code></pre>
  
  <p>I've added the <code>Thread.sleep</code> part to simulate downloading data. The code in the <code>onPostExecute</code> is to simulate use of the <code>Fragment</code>.</p>
  
  <p>When I rotate the screen very fast between landscape and portrait, I get an Exception at the <code>onPostExecute</code> code: </p>
  
  <blockquote>
    <p>java.lang.IllegalStateException: Fragment MyFragment{410f6060} not
    attached to Activity</p>
  </blockquote>
  
  <p>I think it's because a new <code>MyFragment</code> has been created in the meantime, and was attached to the Activity before the <code>AsyncTask</code> finished. The code in <code>onPostExecute</code> calls upon a unattached <code>MyFragment</code>.</p>
  
  <p>But how can I fix this?</p>