title: Adding child fragments in a separate thread
body: <p>I'm having an issue with adding a child fragment to another fragment in my android app. </p>
  
  <p>In the parent fragment I have something like this: </p>
  
  <pre><code>    @Override
      public View onCreateView(blah) {
          Thread initializeStateThread = new Thead() {
              public void run() {
                  // Make some requests to get some data that determines
                  // which child fragment to display
                  state = // Some state that corresponds to a child frag
  
                  // Method that set's the child fragment based on |state|
                  setChildFragment(state);
              }
          } 
          initializeStateThread.start()
  
          // Do a bunch of other stuff
  
          return view;
      }
  
      setChildFragment(State state) {
          Fragment fragment = null;
          switch (mState) {
              case CASE_1:
                  fragment = new DriverFindSpotFragment();
                  break;
              case CASE_2:
                  fragment = new DriverNavigationFragment();
                  break;
              case CASE_3:
                  fragment = new DriverArriveSpotFragment();
                  break;
          }
  
          getActivity().runOnUiThread(new Runnable() {
          @Ovverride
          public void run() {
              getChildFragmentManager().beginTransaction().
                 replace(R.id.driver_fragment_container, fragment).commit();
          };
      }
  </code></pre>
  
  <p>However, this only works about 50% of the time. The other 50% of the time the child fragment just doesn't show up (there isn't an error or anything). I think the problem is related to the fact that I'm attempting to add a child fragment after the parent's view is returned form onCreateView. </p>
  
  <p>I've noticed that if I force the main thread to sleep for a second or two, the issue goes away (I assume because the child fragment gets set before the view is returned in that case). </p>
  
  <p>Has anyone ever run into a similar issue? Or can anyone confirm my theory as to why this issue is happening? </p>