title: onCreate sometimes running in background
body: <p>I'm not quite sure how to debug the phenomenon I'm currently seeing in my Android application.
  I have an Activity which is just doing some networking stuff (which needs to be done in background).
  This activity is launched from a PreferencesFragment using an Intent.
  When the user selects the preference item, the Intent is fired and the Activity is started (then it does the networking stuff and quits using finish()).</p>
  
  <p>I created an AsyncTask to perform the networking actions in the background.
  (I thought that onCreate will most probably run in the UI thread...)
  But then, an exception occurred:
  java.lang.RuntimeException:  Can't create handler inside thread that has not called Looper.prepare()</p>
  
  <p>Did onCreate() already run in the background???</p>
  
  <p>To test that, I moved the networking functions directly into onCreate().
  This was working well...
  ... at least several times.</p>
  
  <p>Suddenly, an exception was thrown:
  java.lang.RuntimeException: Unable to start activity ComponentInfo{...}: android.os.NetworkOnMainThreadException</p>
  
  <p>Moving the code back to the AsyncTask helped... for some time.</p>
  
  <p>Does anyone know why this phenomenon might occur?
  Are there scenarios when onCreate() runs in the UI thread and others when onCreate() runs in background?</p>
  
  <p>My class is as simple as this:</p>
  
  <pre><code>public class ReregisterInDb extends Activity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          new ReregisterAsyncTask().execute(""); // solution 1
  
              // solution 2
          //GCMFunctions gcmFunctions = new GCMFunctions(getApplicationContext());
          //gcmFunctions.registerInDb();
  
          super.onCreate(savedInstanceState);
          finish();
      }
  
      class ReregisterAsyncTask extends AsyncTask&lt;String, Void, Void&gt; {
  
          @Override
          protected Void doInBackground(String... params) {
              GCMFunctions gcmFunctions = new GCMFunctions(getApplicationContext());
              gcmFunctions.registerInDb();
              return null;
          }
  
      }
  
  }
  </code></pre>