title: Generic asyncTask with callback (for web services)
body: <p>I've already developed many Android apps that make web service requests, always with the following approach:</p>
  
  <p>In every activity that need to make a web service request, I define an inner <code>AsyncTask</code> that shows a <code>ProgressDialog</code> in <code>onPreExecute()</code>, makes the web service call in <code>doInBackground</code>, and dismisses the progressDialog and updates the results in the UI from <code>onPostExecute()</code>.</p>
  
  <p>My concern is: Is there a better (shorter) way to do it? Does it make sense to repeat all that code in every activity? I've been googling a lot, but I've found nothing.</p>
  
  <p>My question is: Couldn't I define a Callback interface? for example this one:</p>
  
  <pre><code>public interface RequestCallback {
      public void onSuccess(Whatever whatever);
      public void onError(ErrorCode errorCode, String message);
  }
  </code></pre>
  
  <p>... and then define an external class, for example <code>AsyncRequest</code>, that wraps the AsyncTask definition and the ProgressDialog <code>show()</code> and <code>dismiss()</code> statements. So, all activities would just need to instantiate that class, and pass in the following parameters:</p>
  
  <p>1) The method of the web service to run</p>
  
  <p>2) A Bundle with all the parameters of that method of the web service</p>
  
  <p>3) A RequestCallback instance (that could be an anonymous inline instance, where I could update the UI from <code>onSuccess()</code>)</p>
  
  <p>4) The context of the Activity (necessary to show the ProgressDialog(), so I would still need a way to prevent configuration change exceptions and so...), </p>
  
  <p>Do you find this a good design? It could save hundreds of lines of code...</p>