title: Fragment no longer exists for key android:target_state
body: <p>I think I have same problem <a href="https://stackoverflow.com/questions/17935224/fragement-no-longer-exists-for-key-f0-index-1">here</a> but there is no answer. </p>
  
  <p>I have an Activity with an ViewPager and a FrameLayout which will contain a Fragment. It look like this :</p>
  
  <pre><code>Activity
     |— ViewPager 
            |-FragmentA
     |— Framelayout 
          |— FragmentB
                |— FragmentC (here I called fragmentC.setTargetFragment method).
  </code></pre>
  
  <p>When I rotate the device. I 'll get this error :</p>
  
  <pre><code>E/AndroidRuntime(10354): Caused by: java.lang.IllegalStateException: Fragment no longer exists for key android:target_state: index 1
  E/AndroidRuntime(10354):    at android.support.v4.app.FragmentManagerImpl.getFragment(FragmentManager.java:586)
  E/AndroidRuntime(10354):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:885)
  E/AndroidRuntime(10354):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1136)
  E/AndroidRuntime(10354):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1118)
  E/AndroidRuntime(10354):    at android.support.v4.app.FragmentManagerImpl.dispatchCreate(FragmentManager.java:1922)
  E/AndroidRuntime(10354):    at android.support.v4.app.Fragment.performCreate(Fragment.java:1776)
  E/AndroidRuntime(10354):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:915)
  E/AndroidRuntime(10354):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1136)
  E/AndroidRuntime(10354):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1118)
  E/AndroidRuntime(10354):    at android.support.v4.app.FragmentManagerImpl.dispatchCreate(FragmentManager.java:1922)
  E/AndroidRuntime(10354):    at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:266)
  E/AndroidRuntime(10354):    at com.example.android.animationsdemo.MyActivity.onCreate(MyActivity.java:20)
  </code></pre>
  
  <p>Everything work fine if I don't call fragmentC.setTargetFragment method or I don't set the Adapter for the Viewpager. 
  Here are my Code :</p>
  
  <p>Activity :</p>
  
  <pre><code>public class MyActivity extends FragmentActivity{
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          setContentView(R.layout.my_activity);
  
          ViewPager mViewPager = (ViewPager) findViewById(R.id.pager);
  
          TabsAdapter tabsAdapter = new TabsAdapter(this);
          mViewPager.setAdapter(tabsAdapter);
  
          findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  MyFragmentB fragmentA = new MyFragmentB();
                  getSupportFragmentManager().beginTransaction().add(R.id.flContainer, fragmentA,
                          "TAG").addToBackStack("back").commit();
              }
          });
  
  
      }
  
      public static class TabsAdapter extends FragmentPagerAdapter {
  
          private final Context mContext;
          private final ArrayList&lt;TabInfo&gt; mTabs = new ArrayList&lt;TabInfo&gt;();
  
          static final class TabInfo {
              private final Class&lt;?&gt; clss;
              private final Bundle args;
  
              TabInfo(Class&lt;?&gt; _class, Bundle _args) {
                  clss = _class;
                  args = _args;
              }
          }
  
          public TabsAdapter(FragmentActivity activity) {
              super(activity.getSupportFragmentManager());
              mContext = activity;
  
              TabInfo tabInfo = new TabInfo(MyFragmentA.class, null);
              mTabs.add(tabInfo);
          }
  
          @Override
          public Fragment getItem(int position) {
              TabInfo info = mTabs.get(position);
              return Fragment.instantiate(mContext, info.clss.getName(), info.args);
          }
  
          @Override
          public int getCount() {
              return mTabs.size();
          }
      }
  
  }
  </code></pre>
  
  <p>The Activity layout :</p>
  
  <pre><code> &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical"&gt;
  
      &lt;Button
          android:id="@+id/button"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:text="Add Fragment B"/&gt;
  
      &lt;FrameLayout
          android:layout_width="match_parent"
          android:layout_height="300dip"
          android:id="@+id/flContainer" &gt;
  
      &lt;TextView
          android:layout_width="match_parent"
          android:layout_height="200dp"
          android:text="wrtadfa"/&gt;
      &lt;/FrameLayout&gt;
  
      &lt;android.support.v4.view.ViewPager
          android:id="@+id/pager"
          android:layout_below="@+id/rlContainer"
          android:layout_width="match_parent"
          android:layout_height="300dip" /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>And FragmentA :</p>
  
  <pre><code>public class MyFragmentA extends Fragment {
  
      @Override
      public void onAttach(Activity activity) {
          super.onAttach(activity);
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          View myFragmentView = inflater.inflate(R.layout.fragment_a, container, false);
          return myFragmentView;
      }
  }
  </code></pre>
  
  <p>FragmentA layout :</p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:orientation="vertical" &gt;
  
     &lt;TextView
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:text="It's Fragment A" /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>FragmentB :</p>
  
  <pre><code>public class MyFragmentB extends Fragment implements MyFragmentC.CallBack {
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          View myFragmentView = inflater.inflate(R.layout.fragment_b, container, false);
  
          myFragmentView.findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  MyFragmentC fragmentA = new MyFragmentC();
  
                  //This caused the problem
                  fragmentA.setTargetFragment(MyFragmentB.this, 0);
  
                  getChildFragmentManager().beginTransaction().add(R.id.flContainer2, fragmentA, "TAG").addToBackStack("back").commit();
              }
          });
  
          return myFragmentView;
      }
  }
  </code></pre>
  
  <p>fragment_b.xml :</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:background="#888888"
     android:orientation="vertical" &gt;
  
     &lt;TextView
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:text="It's Fragment B" /&gt;
  
     &lt;TextView
         android:id="@+id/c_received"
         android:layout_width="match_parent"
         android:layout_height="wrap_content" /&gt;
  
      &lt;Button
          android:id="@+id/button"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:text="Add Fragment C"/&gt;
  
      &lt;FrameLayout
          android:id="@+id/flContainer2"
          android:layout_width="match_parent"
          android:layout_height="match_parent"&gt;
  
      &lt;/FrameLayout&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>FragmentC :</p>
  
  <pre><code>public class MyFragmentC extends Fragment {
  
      public static interface CallBack{
          //some call back
      }
  
      @Override
      public void onAttach(Activity activity) {
          super.onAttach(activity);
  
          if(getTargetFragment() != null){
              CallBack callBack = (CallBack) getTargetFragment();
          }
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          View myFragmentView = inflater.inflate(R.layout.fragment_c, container, false);
  
          return myFragmentView;
      }
  }
  </code></pre>
  
  <p>fragment_c.xml :</p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:background="#888888"
     android:orientation="vertical" &gt;
  
     &lt;TextView
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:text="It's Fragment C" /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>I know that I can work around by let the Activity implement the MyFragmentC.CallBack instead of FragmentB implement it.
  But I just curious why does it happen ???
  If I don't set Adapter for the viewpager or don't call "setTargetFragment" in FragmentC. Then everything work fine. </p>
  
  <p>So sorry because I don't know how to format the code well in here and it's a long post. Any help could be appreciate.</p>