title: Why don't AsyncTasks always work within an onClickListener?
body: <p>I have noticed a this over the last few months. If I set a clickListener on a button to execute an AsyncTask, it doesn't always get executed. I solve the problem by wrapping the line of code that executes the AsyncTask in a method.</p>
  
  <p>Here's the code that doesn't work all of the time:</p>
  
  <pre><code>button.setOnClickListener(new OnClickListener() {
          @Override
          public void onClick(View v) {
              new CustomAsyncTask().execute();
          }
      });
  </code></pre>
  
  <p>Here's my fix:</p>
  
  <pre><code>button.setOnClickListener(new OnClickListener() {
          @Override
          public void onClick(View v) {
              executeAsyncTask();
          }
      });
  
  public void executeAsyncTask(){
      new CustomAsyncTask().execute();
  }
  </code></pre>
  
  <p>I  haven't noticed the pattern as to when it does and doesn't break, but I'm wondering if anyone else has noticed this?</p>
  
  <p>Also, if anyone has an explanation as to why this happens that would be cool too.</p>
  
  <p>Thanks in advance.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Here's the CustomAsyncTask (I know it's not being called all of the time because it doesn't print anything)</p>
  
  <pre><code>private class CustomAsyncTask extends AsyncTask&lt;Void, Void, Void&gt;{
  
      @Override
      protected Void doInBackground(Void... params) {
  
          Log.i("CustomAsyncTask", "doInBackground");
  
          return null;
      }
  }
  </code></pre>