title: Passing a handler from a background Handler Thread, to background thread
body: <p>Can anyone point me in the right direction here please ? </p>
  
  <p>I have an activity which spawns two threads, a thread for handling messages, using a Looper</p>
  
  <pre><code>    public static class MiddleThread extends Handler{
      static public Handler handler;
  
      public void run() {
          Looper.prepare();
          Log.d("MiddleThread", "Looper is prepared !");
          handler = new Handler() {
  
              public void handleMessage(Message msg)
              {
                  Bundle bundle = msg.getData();
                  String exitString = bundle.getString("endmessage");
                  if(exitString.equals(("ExitOK")))
                  {
                      boolean searchFinished = true;
                      Looper looper = Looper.myLooper();
                      looper.quit();
                  } else
                  {
                      int fileCount = bundle.getInt("filecount");
                      String fileName = bundle.getString("filename");
                      Log.d("MiddleThread", "File Number " + fileCount + " is " + fileName);
                  }
              }
          };
          Log.d("MiddleThread", "nandler should be initialised");
          Looper.loop();
      }
  </code></pre>
  
  <p>... then it spawns the main Worker Thread, which is passed a handler from the UI Thread, and the handler from the above thread.</p>
  
  <pre><code>public class BasicSearch {
  public Handler handlerUi, handlerMiddleThread;
  public Message messageUi, messageMiddleThread;
  public int fileCount = 0;
  public BasicSearch(Handler ui, Handler mt) {
      handlerUi = ui;
      handlerMiddleThread = mt;
  }
  
  public void listFiles()
  {
      File searchPath = Environment.getExternalStorageDirectory();
      messageUi = handlerUi.obtainMessage();
      messageMiddleThread = handlerMiddleThread.obtainMessage();
      walk(searchPath);
      Bundle b = new Bundle();
      b.putString("endmessage", "ExitOK");
      messageMiddleThread.setData(b);
      handlerMiddleThread.dispatchMessage(messageMiddleThread);
  }
  
  private void walk(File path) {
      File[] list = path.listFiles();
      for(File f : list)
      {
          if(f.isDirectory())
          {               
              walk(new File(f.getAbsolutePath()));
          } else {
              processFile(f);
          }
      }
  }
  
  private void processFile(File f) {
      Bundle b = new Bundle();
      fileCount++;
      b.putString("filename", f.getName());
      b.putInt("filecount", fileCount);
      messageMiddleThread.setData(b);
      Log.d("BasicSearch", "Data is set, to send to MiddleThread");
      handlerMiddleThread.dispatchMessage(messageMiddleThread);
      Log.d("BasicSearch", "Message sent");
  
  }
  
      }
  </code></pre>
  
  <p>Whatever happens, when it tries to dispatchMessage, handlerMiddleThread reverts to being null. I even have the following code in my activity, to try and ensure that it isn't null, but it still ends up being null when I get to send the message. </p>
  
  <pre><code>      startMiddleThread();
      while(true)
      {
          if(MiddleThread.handler != null)
              break;
      }   
      startSearchThread();
  </code></pre>
  
  <p>This is a test project, as I wanted to be able to get the Handler/Looper concept properly understood before continuing on with my project.</p>
  
  <p>I have successfully managed to use a Handler in my UI Threads before, but my current project has too much processing going on in the UI, and I want to have a secondary thread handling the output from the searchThread, and just receive a message in UI thread when the thread is complete.</p>