title: IllegalStateException: Fragment <ThisFragment> is not currently in the FragmentManager
body: <p>I have trouble of tracing the cause of the crash in my app. I have a fragment that displays a <code>ListView</code>, I also have <code>SlidingMenu</code> for a search field. And when I search, the <code>ListView</code> items are repopulated basing from the search results. Now, my app crashes when I try to open a <code>ListView</code> item from the new populated result.</p>
  
  <p>The exception is triggered here:</p>
  
  <pre><code>@Override
  public void onSaveInstanceState(Bundle outState) {
      super.onSaveInstanceState(outState);
      getSupportFragmentManager().putFragment(outState, "mContent", mContent); //Exception made here
  }
  </code></pre>
  
  <p>Is this because of the support library? How do I resolve this?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Here's the stack trace:</p>
  
  <pre><code>02-05 12:51:53.941: E/AndroidRuntime(11638): FATAL EXCEPTION: main
  02-05 12:51:53.941: E/AndroidRuntime(11638): java.lang.IllegalStateException: Fragment MyFragment{436749b0} is not currently in the FragmentManager
  02-05 12:51:53.941: E/AndroidRuntime(11638):    at android.support.v4.app.FragmentManagerImpl.putFragment(FragmentManager.java:546)
  02-05 12:51:53.941: E/AndroidRuntime(11638):    at com.sample.android.app.SearchListActivity.onSaveInstanceState(SearchListActivity.java:118)
  02-05 12:51:53.941: E/AndroidRuntime(11638):    at android.app.Activity.performSaveInstanceState(Activity.java:1137)
  02-05 12:51:53.941: E/AndroidRuntime(11638):    at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1215)
  02-05 12:51:53.941: E/AndroidRuntime(11638):    at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:2972)
  02-05 12:51:53.941: E/AndroidRuntime(11638):    at android.app.ActivityThread.handleStopActivity(ActivityThread.java:3031)
  02-05 12:51:53.941: E/AndroidRuntime(11638):    at android.app.ActivityThread.access$900(ActivityThread.java:138)
  02-05 12:51:53.941: E/AndroidRuntime(11638):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
  02-05 12:51:53.941: E/AndroidRuntime(11638):    at android.os.Handler.dispatchMessage(Handler.java:99)
  02-05 12:51:53.941: E/AndroidRuntime(11638):    at android.os.Looper.loop(Looper.java:213)
  02-05 12:51:53.941: E/AndroidRuntime(11638):    at android.app.ActivityThread.main(ActivityThread.java:4787)
  02-05 12:51:53.941: E/AndroidRuntime(11638):    at java.lang.reflect.Method.invokeNative(Native Method)
  02-05 12:51:53.941: E/AndroidRuntime(11638):    at java.lang.reflect.Method.invoke(Method.java:511)
  02-05 12:51:53.941: E/AndroidRuntime(11638):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
  02-05 12:51:53.941: E/AndroidRuntime(11638):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
  02-05 12:51:53.941: E/AndroidRuntime(11638):    at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p><strong>EDIT 2:</strong>
  Here's my Activity:</p>
  
  <pre><code>private Fragment mFragment;
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  
              ..........
  
      params = new Bundle();
      params.putInt("id", id);
  
      // set the Above View Fragment
      if (savedInstanceState != null)
          mFragment = getSupportFragmentManager().getFragment(
                  savedInstanceState, "mFragment");
      else {
          FragmentManager fragmentManager = getSupportFragmentManager();
          FragmentTransaction fragmentTransaction = fragmentManager
                  .beginTransaction();
          SampleListFragment fragment = new SampleListFragment();
          fragment.setArguments(params);
          fragmentTransaction.add(R.id.list_fragment, fragment);
          fragmentTransaction.commit();
      }
  
      if (mFragment == null) {
          mFragment = new SampleListFragment();
          mFragment.setArguments(params);
      }
      getSupportFragmentManager().beginTransaction()
              .replace(R.id.list_fragment, mFragment).commit();
  
      // set the Behind View Fragment
      Fragment searchFragment = new SampleSearchFragment();
      searchFragment.setArguments(params);
  
      getSupportFragmentManager().beginTransaction()
              .replace(R.id.activity_search, searchFragment)
              .commit();
  }
  
  
  @Override
  public void onSaveInstanceState(Bundle outState) {
      super.onSaveInstanceState(outState);
      getSupportFragmentManager().putFragment(outState, "mFragment",
              mFragment);
  }
  </code></pre>
  
  <p>And in my <code>SampleListFragment</code>, I have this method:</p>
  
  <pre><code>public static boolean isNetworkAvailable(Activity activity) {
      ConnectivityManager connectivity = (ConnectivityManager) activity
              .getSystemService(Context.CONNECTIVITY_SERVICE);
      if (connectivity == null) {
          return false;
      } else {
          NetworkInfo[] info = connectivity.getAllNetworkInfo();
          if (info != null) {
              for (int i = 0; i &lt; info.length; i++) {
                  if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                      return true;
                  }
              }
          }
      }
      return false;
  }
  </code></pre>
  
  <p>Inside of <code>Asynctask</code>:</p>
  
  <pre><code>..........
  @Override
  protected void onPostExecute(String result) {
      super.onPostExecute(null);
  
      if (null != pDialog &amp;&amp; pDialog.isShowing()) {
          pDialog.dismiss();
      }
  
      if (isNetworkAvailable(getActivity())) { //Triggered NPE
  </code></pre>
  
  <p>From <a href="https://stackoverflow.com/a/19915714/1968739">here</a>, how do I know if <code>getActivity()</code> is now attached to the parent activity before calling it?</p>