title: Updating Views from non-UI threads
body: <p>I am confused about how the Android system works, specially when it updates the view hierarchy. <strong>We all know that we should not update any view from any thread other than UI (Main) thread</strong>. And even <strong>Android system throws exception</strong> when we try to do so. 
  The other day I was trying to implement a custom progress showing view in my application. So I started with using standard Java threads and handler combo. </p>
  
  <p>What I found surprised me as I was able to update a TextView from background thread. </p>
  
  <pre><code>new Thread(new Runnable() {
  
          @Override
          public void run() {
              mTextView.setText("I am " + Thread.currentThread().getName());
          }
      }).start();
  </code></pre>
  
  <p>After which I tried updating other views also, which worked pretty well. So I tried putting a sleep call in background Thread.</p>
  
  <pre><code>new Thread(new Runnable() {
  
          @Override
          public void run() {
              mTextView.setText("Thread : before sleep");
              try {
                  Thread.sleep(1000);
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
              mTextView.setText("Thread : after sleep");
          }
      }).start();
  </code></pre>
  
  <p>And it crashes as expected saying  </p>
  
  <blockquote>
    <p>android.view.ViewRootImpl$CalledFromWrongThreadException: Only the
    original thread that created a view hierarchy can touch its views.</p>
  </blockquote>
  
  <p>Then I tried putting the setText() calls in <strong>loop for 100, 1000 times</strong> before and after the sleep() call. Of course the app crashes every single time but I was able to see the "Before Sleep" text on my textview. </p>
  
  <p>So my question is <strong><em>when does system detect that some non-UI thread is trying to update the view</em></strong>. And <strong><em>why does not it work when there is no sleep() call in non-UI thread</em></strong> ?</p>