title: Android - Activity onCreate and Fragment onCreate
body: <p>I'm still new to Android programming, so this question is rather basic. I see lots of code samples on the Internet where UI components, like a <code>TextView</code>, are being initialised and accessed in the <code>onCreate()</code> method of an Activity.</p>
  
  <p>When I use Android Studio to create a new project - <code>FirstApp</code> - with default settings, a blank Activity called <code>MainActivity</code> bringing along with it <code>activity_main</code> and <code>fragment_main</code>, I can immediately compile this into an APK, deploy it on my device, and I get a screen with a header "FirstApp" and a TextView in the top left showing "Hello world!".</p>
  
  <p>If I give the <code>TextView</code> an ID of <code>textview1</code>, and create a member variable, <code>TextView myTextView;</code>, then I can reference it in the <code>onCreate()</code> method of the Activity, like so (no compiler errors, of course):</p>
  
  <pre><code>    @Override
      protected void onCreate (Bundle savedInstanceState)
      {
          super.onCreate (savedInstanceState);
          setContentView (R.layout.activity_main);
          myTextView = (TextView) findViewById (R.id.textview1);
          myTextView.setText ("Hello tablet!");
  
          if (savedInstanceState == null)
          {
              getSupportFragmentManager().beginTransaction().add (R.id.container,new PlaceholderFragment()).commit();
          }
      }
  </code></pre>
  
  <p>However, if I compile and run the APK it results in an "Unfortunately, FirstApp has stopped." message.</p>
  
  <p>I have previously got around this issue by moving startup code that accesses UI components into the <code>onStart()</code> method of the Activity, like so:</p>
  
  <pre><code>    @Override
      protected void onStart()
      {
          super.onStart();
          myTextView = (TextView) findViewById (R.id.textview1);
          myTextView.setText ("Hello tablet!");
      }
  </code></pre>
  
  <p>Which would result in a working APK with a single TextView in the top left showing "Hello tablet!" - my simple questions are two-fold...</p>
  
  <ol>
  <li>If the project uses Fragments then should I fully expect that the UI components cannot be accessed in the <code>onCreate()</code> method of the Activity, as I see happening with lots of sample code on the Internet, probably because they've not been created yet?</li>
  <li>Is it acceptable for me to be accessing UI components within the <code>onStart()</code> method of the Activity (which <em>does</em> work) - or should I be doing something else? Previously I have also used the <code>onCreateView</code> method of the Fragment, but is the best place to access UI components inside a Fragment actually in the <code>onCreate()</code> method of the Fragment, which I have not yet tried?</li>
  </ol>
  
  <p>I also note that the <code>onCreate()</code> method of the default Fragment Android Studio creates for you when you create a new project does not have a stub provided... but <code>onCreateView</code> does, and the lifecycle documentation implies (to me, anyway) that this <em>might</em> be the best place to be doing things like this.</p>
  
  <p>Any guidance on this is appreciated.</p>