title: Calling same dialog from different threads
body: <p>I have an android application with different activities and they all pull data from a web source. This is done by implementing Runnable and creating a thread with the activity as object. The basic class looks like this:</p>
  
  <pre><code>public ActivityX extends Activity implements Runnable {
  
      @Override
      public onResume() {
          super.onResume();
          Thread someThread = new Thread(this);
          someThread.start();
      }
  
      @Override
      public run() {
          try {
              // pull web content
          }
          catch(TimeOutException e) {
              // &gt;&gt;&gt; create dialog here &lt;&lt;&lt;
              // go back to another activity
          }
      }
  }
  </code></pre>
  
  <p>I tried to create a dialog helper class with a static method that returns the timeout dialog and then call show() like this:</p>
  
  <pre><code>HelperClass.getTimeOutDialog().show();
  </code></pre>
  
  <p>but the problem is, I can't call it from inside the run() method, as it's in a different thread. If I try to, I will get a runtime exception stating:</p>
  
  <p><strong>Can't create handler inside thread that has not called Looper.prepare()</strong></p>
  
  <p>I need to do this dialog for nearly a dozen of activities and I really want to get around using a Handler objects and sending a message to call the dialog every time. Isn't there an easier way to do this? I just can't think of any right now unfortunately.</p>
  
  <p>My code would look something like this:</p>
  
  <pre><code>handler.handleEmptyMessage(1);
  </code></pre>
  
  <p>This is to call the handler. And the following would handle the message:</p>
  
  <pre><code>private Handler handler = new Handler() {
      @Override
      public void handleMessage(Message msg) {
          if(msg.what == 1) {
              // show dialog here
          }
      }
  };
  </code></pre>
  
  <p>Cheers</p>