title: java.lang.IllegalStateException: Fragment no longer exists for key f0: index 1
body: <p>I am having couple of fragment in an Activity. After doing some process I am closing the fragment using the below code.</p>
  
  <pre><code>if (getActivity().getSupportFragmentManager().getBackStackEntryCount() &gt; 0) {
                          getActivity().getSupportFragmentManager().popBackStackImmediate();
  }
  </code></pre>
  
  <p>But on line popBackStackImmediate() its throwing the error</p>
  
  <pre><code>    Process: com.TestProject.testpro, PID: 17966
   java.lang.IllegalStateException: Fragment no longer exists for key f0: index 1
       at android.support.v4.app.FragmentManagerImpl.getFragment(FragmentManager.java:879)
       at android.support.v4.app.FragmentStatePagerAdapter.restoreState(FragmentStatePagerAdapter.java:215)
       at android.support.v4.view.ViewPager.onRestoreInstanceState(ViewPager.java:1481)
       at android.view.View.dispatchRestoreInstanceState(View.java:14746)
       at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3121)
       at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3127)
       at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3127)
       at android.view.View.restoreHierarchyState(View.java:14724)
       at android.support.v4.app.Fragment.restoreViewState(Fragment.java:475)
       at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1329)
       at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528)
       at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595)
       at android.support.v4.app.BackStackRecord.executePopOps(BackStackRecord.java:807)
       at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2360)
       at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2149)
       at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2103)
       at android.support.v4.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:823)
       at android.support.v4.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:776)
       at com.TestProject.testpro.application.fragments.WFMUpgradeToAffinityFragmentAN$WFMStoreProfileIdReceiver.onReceive(WFMUpgradeToAffinityFragmentAN.java:1064)
       at android.support.v4.content.LocalBroadcastManager.executePendingBroadcasts(LocalBroadcastManager.java:297)
       at android.support.v4.content.LocalBroadcastManager.access$000(LocalBroadcastManager.java:46)
       at android.support.v4.content.LocalBroadcastManager$1.handleMessage(LocalBroadcastManager.java:116)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:148)
       at android.app.ActivityThread.main(ActivityThread.java:5417)
       at java.lang.reflect.Method.invoke(Native Method)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
  </code></pre>
  
  <p>There is no other crash which close the fragment. I cant find the exact solution for this issue. Please help me on this.</p>
  
  <p>Edited: </p>
  
  <p>I am using the below function to call the fragment.</p>
  
  <pre><code>void showContentFragment(Fragment newFragment, String backStackName) {
      //Log.e(TAG, "showContentFragment- " + "BackstackName: "+ backStackName);
      FragmentManager mFragmentManager = getActivity().getSupportFragmentManager();
      if (newFragment != null) {
          //updateNavigation(backStackName);
          FragmentTransaction ft = mFragmentManager.beginTransaction();
          ft.replace(R.id.fragment_container, newFragment);
          if (backStackName != null &amp;&amp; !backStackName.isEmpty()) {
              ft.addToBackStack(backStackName);
          }
  
          ft.commit();
      }
  }
  </code></pre>