title: Add Nested Fragment after Parent Fragment view is already created
body: <p>I am trying to create a Fragment which has a public method for adding child Fragments to itself.  </p>
  
  <p>I've been reading through potentially similar questions but haven't found anything to help so far. I've reduced the problem down to a simple test app shown below.  </p>
  
  <p>Once <code>fragA</code> has been added to the main layout, I call the public method <code>fragA.addFragB()</code> to get it to add an instance of FragmentClassB to itself, but this causes the test app to crash, indicating "Activity has been destroyed" (see LogCat at the end of the post). Does this mean <code>fragA</code> has been destroyed so I can't add <code>fragB</code> to it, or does it mean <code>fragB</code> has been destroyed so I can't add it to <code>fragA</code>? Or does it mean something else entirely?</p>
  
  <p><strong>MainActivity.java</strong>  </p>
  
  <pre><code>public class MainActivity extends FragmentActivity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          FragmentManager fragMan = getSupportFragmentManager();
  
          // add Fragment A to the main linear layout
          FragmentTransaction fragTrans = fragMan.beginTransaction();
          FragmentClassA fragA = new FragmentClassA();
          fragTrans.add(R.id.mainLinearLayout, fragA);
          fragTrans.addToBackStack("A");
          fragTrans.commit();
  
          // get Fragment A to add a Fragment B to itself
          fragA.addFragB();
      }
  
  }
  </code></pre>
  
  <p><strong>activity_main.xml</strong>  </p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:paddingBottom="@dimen/activity_vertical_margin"
      android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      tools:context=".MainActivity" &gt;
  
      &lt;LinearLayout
          android:id="@+id/mainLinearLayout"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_alignParentLeft="true"
          android:layout_alignParentTop="true"
          android:orientation="horizontal" &gt;
      &lt;/LinearLayout&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p><strong>FragmentClassA.java</strong>  </p>
  
  <pre><code>public class FragmentClassA extends Fragment {
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState) {
          return inflater.inflate(R.layout.fragment_a, container, false);
      }
  
      public void addFragB() {
          FragmentManager childFragMan = getChildFragmentManager();
  
          FragmentTransaction childFragTrans = childFragMan.beginTransaction();
          FragmentClassB fragB = new FragmentClassB();
          childFragTrans.add(R.id.fragA_LinearLayout, fragB);
          childFragTrans.addToBackStack("B");
          childFragTrans.commit();
  
      }
  }
  </code></pre>
  
  <p><strong>fragment_a.xml</strong>  </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/fragA_LinearLayout"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="horizontal" &gt;
  
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p><strong>FragmentClassB.java</strong>  </p>
  
  <pre><code>public class FragmentClassB extends Fragment {
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState) {
          return inflater.inflate(R.layout.fragment_b, container, false);
      }
  
  }
  </code></pre>
  
  <p><strong>fragment_b.xml</strong>  </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="match_parent"
      android:layout_height="match_parent"
      android:orientation="horizontal" &gt;
  
      &lt;Button
          android:id="@+id/button1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Button" /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p><strong>LogCat</strong>  </p>
  
  <pre><code>11-18 16:17:05.627: E/AndroidRuntime(14351): FATAL EXCEPTION: main
  11-18 16:17:05.627: E/AndroidRuntime(14351): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nestedfragmenttest/com.example.nestedfragmenttest.MainActivity}: java.lang.IllegalStateException: Activity has been destroyed
  11-18 16:17:05.627: E/AndroidRuntime(14351):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
  11-18 16:17:05.627: E/AndroidRuntime(14351):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
  11-18 16:17:05.627: E/AndroidRuntime(14351):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
  11-18 16:17:05.627: E/AndroidRuntime(14351):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
  11-18 16:17:05.627: E/AndroidRuntime(14351):    at android.os.Handler.dispatchMessage(Handler.java:99)
  11-18 16:17:05.627: E/AndroidRuntime(14351):    at android.os.Looper.loop(Looper.java:137)
  11-18 16:17:05.627: E/AndroidRuntime(14351):    at android.app.ActivityThread.main(ActivityThread.java:5103)
  11-18 16:17:05.627: E/AndroidRuntime(14351):    at java.lang.reflect.Method.invokeNative(Native Method)
  11-18 16:17:05.627: E/AndroidRuntime(14351):    at java.lang.reflect.Method.invoke(Method.java:525)
  11-18 16:17:05.627: E/AndroidRuntime(14351):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
  11-18 16:17:05.627: E/AndroidRuntime(14351):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
  11-18 16:17:05.627: E/AndroidRuntime(14351):    at dalvik.system.NativeStart.main(Native Method)
  11-18 16:17:05.627: E/AndroidRuntime(14351): Caused by: java.lang.IllegalStateException: Activity has been destroyed
  11-18 16:17:05.627: E/AndroidRuntime(14351):    at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1365)
  11-18 16:17:05.627: E/AndroidRuntime(14351):    at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
  11-18 16:17:05.627: E/AndroidRuntime(14351):    at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574)
  11-18 16:17:05.627: E/AndroidRuntime(14351):    at com.example.nestedfragmenttest.FragmentClassA.addFragB(FragmentClassA.java:26)
  11-18 16:17:05.627: E/AndroidRuntime(14351):    at com.example.nestedfragmenttest.MainActivity.onCreate(MainActivity.java:25)
  11-18 16:17:05.627: E/AndroidRuntime(14351):    at android.app.Activity.performCreate(Activity.java:5133)
  11-18 16:17:05.627: E/AndroidRuntime(14351):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
  11-18 16:17:05.627: E/AndroidRuntime(14351):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
  11-18 16:17:05.627: E/AndroidRuntime(14351):    ... 11 more
  </code></pre>
  
  <p><strong>EDIT:</strong>  </p>
  
  <p>ianhanniballake's answer and GrIsHu's later answer which expands on the same point have both been helpful in pointing out the root of the problem. However this raises a further issue.<br>
  The final intention is that FragmentClassA will be part of a library. It will be used in multiple situations and the number of FragmentClassB instances will vary, or there may even be none. Hence I need to be able to trigger the addition of the child fragments to any instance of FragmentClassA from the parent activity. I've just had a look at keeping <code>fragA</code> as a class level variable in <code>MainActivity</code> and then calling <code>fragA.AddFragB()</code> in the <code>MainActivity</code>'s <code>onActivityCreated()</code> method, but it is not available to be overridden. Any thoughts?</p>