title: Access Fragment View from Activity's onCreate
body: <p>I am in the process of making my first app for Android, and I have a <code>Fragment</code> that gets added to my <code>Activity</code> in the <code>Activity</code>'s <code>onCreate()</code> method. The problem I am facing is that I am unable to find any of the views contained within the <code>Fragment</code> from the <code>Activity</code>'s <code>onCreate()</code> method.</p>
  
  <p>Other threads have suggested that this is because the <code>Fragment</code> has not yet been inflated, so <code>findViewById()</code> will return null for any views contained within the <code>Fragment</code>.</p>
  
  <p>Here is what I mean:</p>
  
  <p>Activity:</p>
  
  <pre><code>@Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      System.out.println("activity onCreate");
      setContentView(R.layout.activity_main);
  
      if (savedInstanceState != null) {
          return;
      }
  
      initialiseUI(); // Fragment added to Activity
  
      System.out.println("end of activity onCreate");
  }
  </code></pre>
  
  <p>Fragment:</p>
  
  <pre><code>@Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) {
  System.out.println("fragment onCreateView");
      return inflater.inflate(R.layout.event_log, container, false);
  }
  </code></pre>
  
  <p>This prints the results:</p>
  
  <p>activity onCreate
  end of activity onCreate
  fragment onCreateView</p>
  
  <p>Because of this order, any attempt to access the views of the <code>Fragment</code> in the <code>Activity</code>'s <code>onCreate()</code> method (using <code>findViewById()</code>) produces a <code>NullPointerException</code>, as the <code>Fragment</code>'s <code>onCreateView()</code> only gets called AFTER the end of the <code>Activity</code>'s <code>onCreate()</code>.</p>
  
  <p>Using the <code>FragmentManger</code>'s <code>executePendingTransactions()</code> after adding the <code>Fragment</code> doesn't help.</p>
  
  <p>Basically, I have been forced to put the problem code in the <code>Activity</code>'s <code>onStart()</code> method instead of <code>onCreate()</code>, as <code>onStart()</code> happens AFTER the <code>Fragment</code>'s <code>onCreateView()</code>.</p>
  
  <p>Does anyone what the standard practice here is, or how I can make my Fragment-View-accessing code work within the <code>Activity</code>'s <code>onCreate()</code> method?</p>