title: Android Handler: Send 8 different messages: handler finds 8 messages but only used the last message
body: <p>I am new to Android and found out that to keep updating the main view I had to create a thread to handle various process and then pass back updates to the main view. I decided to use the Handler class to do this.  The view in this example has a button to activate the code and a tablelayout to display the messages received - representing a stage of the process.</p>
  
  <p>The problem is that I send 8 different messages and I can see 8 messages coming back in the handler, but all 8 have the same contents as message 8 only.</p>
  
  <p>I was expecting the handler to pick the messages up in sequence.</p>
  
  <p>Any suggestions on a better way of doing this always welcome to learn.</p>
  
  <p>Here is the code:</p>
  
  <p>`</p>
  
  <pre><code>public class messageHandlerTest extends Activity {
  
  protected TextView textView;
  protected Handler handler = new Handler() {
          @Override
          public void handleMessage(Message msg) {
              String[] status = (String[]) msg.obj;
              createTableRow(status);
              Log.e("Got a new message",status[0]+":"+status[1]);
          }
      };
  
  Button btnStartProgress;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      textView = (TextView) findViewById(R.id.statusText);
      textView.setText("");
      //Getting response from server with Network SSID and Password
  
      Button connectButton = (Button)findViewById(R.id.connectButton);
      addListenerOnButton();
  } // End of create
  
  protected class connectWiFi extends Thread implements Runnable {
      //tokens1 = new String[0];
      public void run(){
      try {
          String[] messageString = new String[2];
          Message message = handler.obtainMessage();
          messageString[0]="OK";
          messageString[1]="Number 1";
          message.obj = messageString;
          handler.sendMessage(message);
  
          message = handler.obtainMessage();
          messageString[0]="OK";
          messageString[1]= "Number 2";
          message.obj = messageString;
          handler.sendMessage(message);
          message = handler.obtainMessage();
          messageString[0]="OK";
          messageString[1] = "Number 3";
          message.obj = messageString;
          handler.sendMessage(message);
          message = handler.obtainMessage();
          messageString[0]="OK";
          messageString[1] = "Number 4";
          message.obj = messageString;
          handler.sendMessage(message);
  
                      message = handler.obtainMessage();
                      messageString[0] = "OK";
                      messageString[1] = "Number 5";
                      message.obj = messageString;
                      handler.sendMessage(message);
  
                      message = handler.obtainMessage();
                      messageString[0] = "OK";
                      messageString[1] = "Number 6";
                      message.obj = messageString;
                      handler.sendMessage(message);
  
                      message = handler.obtainMessage();
                      messageString[0] = "OK";
                      messageString[1] = "Number 7";
                      message.obj = messageString;
                      handler.sendMessage(message);
  
                      message = handler.obtainMessage();
                      messageString[0] = "OK";
                      messageString[1] = "Number 8";
                      message.obj = messageString;
                      handler.sendMessage(message);
  
      } catch (Exception e) {
          e.printStackTrace();
          Log.e("Exception found","bugger");
      }
      }// End or run
  }// End of class
  
  public void addListenerOnButton() {
  
      btnStartProgress = (Button) findViewById(R.id.connectButton);
      btnStartProgress.setOnClickListener(
              new OnClickListener() {
                  @Override
                  public void onClick(View v) {
                  connectWiFi connectwifi = new connectWiFi();
                  connectwifi.start();
                  }
  
              });
  
  }
  private void createTableRow(String[] stage) {
      TableLayout tl = (TableLayout) findViewById(R.id.statusTable);
      TableRow tr = new TableRow(this);
      LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
      tr.setLayoutParams(lp);
      tr.setBackgroundColor(0xFFFFFFFF);
      TextView tvStatus = new TextView(this);
      tvStatus.setLayoutParams(lp);
      tvStatus.setPadding(2,1,1,2);
      tvStatus.setTextColor(0xFF000000);
      tvStatus.setText(stage[0]); // Status
      TextView tvStage = new TextView(this);
      tvStage.setLayoutParams(lp);
      tvStage.setPadding(2,1,1,2);
      tvStage.setTextColor(0xFF000000);
      tvStage.setText(stage[1]); // Stage
      tr.addView(tvStatus);
      tr.addView(tvStage);
      tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
  }
  @Override
  protected void onDestroy() {
      super.onDestroy();
  }
  </code></pre>
  
  <p>}</p>
  
  <p>The result of this code looks like:</p>
  
  <p><img src="https://i.stack.imgur.com/5P9kt.jpg" alt="enter image description here"></p>