title: Creating threads from Activity.onCreate() that never die
body: <p>I have spent last couple of hours searching for answers here but nothing seems to make it clear for me.</p>
  
  <p>Here's my problem: I have a simple app with a main menu. One of the options retrieves a list of comments from a PHP server, updates the database and then displays a ListView.</p>
  
  <p>Everything functions properly inside. Now, every time I press back and then start the activity again, a new thread is started. I managed to get to more than 50+ waiting threads. I'm using the DDMS tab from Eclipse to monitor the treads.</p>
  
  <p>If I try to call <code>Looper.getLooper().quit()</code> or <code>Looper.myLooper().quit()</code> I get an error saying "main thread not allowed to quit".</p>
  
  <p>What am I doing wrong and where/how should I be stopping the thread?</p>
  
  <p>Here's the code:</p>
  
  <pre><code>public class RequestActivity extends Activity {
  
      ProgressDialog pDialog;
      DatabaseHelper db;
      int userId;
      myCursorAdapter adapter;
      Thread runner = null;
      ListView list;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          userId = myApplication.getInstance().getSession().getUserId();
          setContentView(R.layout.request_list);
  
          db = new myProvider.DatabaseHelper(this);
          Cursor cursor = db.getRequests(userId);
          startManagingCursor(cursor);
          adapter = new myCursorAdapter(RequestActivity.this, cursor);
          list = (ListView) findViewById(R.id.list);
          list.setVisibility(View.INVISIBLE);
          list.setAdapter(adapter);
  
          pDialog = ProgressDialog.show(RequestActivity.this, "", "Loading ...", true, false);
  
          runner = new Thread() {
              Handler handler = new Handler() {
                  @Override
                  public void handleMessage(Message msg) {
                      adapter.getCursor().requery(); 
                  }
              };  
  
              public void run() {
                  Looper.prepare();
                  // ... setup HttpGet
                  try {
                      // ... get HTTP GET response 
                      if (response != null) {
                          // ... update database
                          handler.sendEmptyMessage(0);
                      }
                  } catch(Exception e) {
                      // .. log exception
                  }
                  Looper.loop();
              }
          };
          runner.start();
      }
  
      private static class ViewHolder {
          // .. view objects
      }
  
      private class myCursorAdapter extends CursorAdapter {
          private LayoutInflater inflater;
          public myCursorAdapter(Context context, Cursor c) {
              super(context, c);
              inflater = LayoutInflater.from(context);
          }
  
          @Override
          public void bindView(View view, Context context, Cursor cursor) {
              // .. bind data
              if (cursor.isLast()) {
                  pDialog.dismiss();
                  list.setVisibility(View.VISIBLE);
              }
          }
  
          @Override
          public View newView(Context context, Cursor cursor, ViewGroup parent) {
              View view = inflater.inflate(R.layout.request_list_item, parent, false);
              ViewHolder holder = new ViewHolder();
              // .. set holder View objects
              view.setTag(holder);
              return view;
          }
      }
  
  }
  </code></pre>