title: Why does my thread not do what i think it does?(Android internet over main thread issue)
body: <p>I have a question about the android main thread within android apps. So when i execute the first block of code i get the error: <strong>android.os.NetworkOnMainThreadException</strong>. And i do not understand why, i am starting the thread as well as performing tasks when the thread is started. WHY does the application still get run on the main thread? And why does the code beneath the first block do the trick?</p>
  <pre><code>public class MainActivity extends AppCompatActivity {
      ClientSocket clientSocket = new ClientSocket();
  
      
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          clientSocket.start();
          try {
              clientSocket.startConnection(&quot;192.168.2.5&quot;, 45032);
              clientSocket.sendMessage(&quot;test&quot;);
          } catch (IOException e) {
              e.printStackTrace();
          }
  
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
      }
  } 
  </code></pre>
  <p><strong>What does end up working:</strong></p>
  <pre><code>public class MainActivity extends AppCompatActivity {
      ClientSocket clientSocket = new ClientSocket();
  
      
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          new Thread(() -&gt; {
              try {
                  clientSocket.startConnection(&quot;192.168.2.5&quot;, 45032);
                  clientSocket.sendMessage(&quot;test&quot;);
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }).start();
  
  
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
      }
  }
  </code></pre>