title: One AsyncTask for multiple Activities
body: <p>I'm writting an app that uses WebServices to retrieve data. Initially I had a private <code>AsyncTask</code> class for each activity that needed data from the WebService. But I've decided to make the code simpler by creating <code>AsyncTask</code> as a public class. All works fine, but my problem is when I want to access the retrieved data from the <code>AsyncTask</code>. </p>
  
  <p>For example this is my <code>AsyncTask</code> class.</p>
  
  <pre><code>public class RestServiceTask extends AsyncTask&lt;RestRequest, Integer, Integer&gt; {
  
      /** progress dialog to show user that the backup is processing. */
      private ProgressDialog dialog;
      private RestResponse response;
      private Context context;
  
      public RestServiceTask(Context context) {
          this.context = context;
  
          //...Show Dialog
      }
  
      protected Integer doInBackground(RestRequest... requests) {
          int status = RestServiceCaller.RET_SUCCESS;
          try {
              response = new RestServiceCaller().execute(requests[0]);
          } catch(Exception e) {
              //TODO comprobar tipo error
              status = RestServiceCaller.RET_ERR_WEBSERVICE;
              e.printStackTrace();
          }
  
          return status;
      }
  
      protected void onPreExecute() {
          response = null;
      }
  
      protected void onPostExecute(Integer result) {
  
          if (dialog.isShowing()) {
              dialog.dismiss();
          }
  
          switch (result) {
          case RestServiceCaller.RET_ERR_NETWORK:
              Toast.makeText(
                      context,
                      context.getResources().getString(
                              R.string.msg_error_network_unavailable),
                      Toast.LENGTH_LONG).show();
              break;
          case RestServiceCaller.RET_ERR_WEBSERVICE:
              Toast.makeText(
                      context,
                      context.getResources().getString(
                              R.string.msg_error_webservice), Toast.LENGTH_LONG)
                      .show();
              break;
          default:
              break;
          }
      }
  
      public RestResponse getResponse() throws InterruptedException {
          return response;
      }
  }
  </code></pre>
  
  <p><code>RestServiceCaller</code>, <code>RestRequest</code> and <code>RestResponse</code> are clasess that I've created.
  I'm using the task like this:</p>
  
  <pre><code>RestRequest request = new JSONRestRequest();
  request.setMethod(RestRequest.GET_METHOD);
  request.setURL(Global.WS_USER);
  HashMap&lt;String, Object&gt; content = new HashMap&lt;String, Object&gt;() {
     {
        put(Global.KEY_USERNAME, username.getText().toString());
     }
  };
  request.setContent(content);
  RestServiceTask task = new RestServiceTask(context);
  task.execute(request);
  </code></pre>
  
  <p>This code works fine and is calling the web service correctly, my problem is when I want access to the response. In the <code>AsyncTask</code> I've created the method <code>getResponse</code> but when I use it, it returns a null object because the <code>AsyncTask</code> is still in progress, so this code doesn't work:</p>
  
  <pre><code>//....
  task.execute(request);
  RestResponse r = new RestResponse();
  r = task.getResponse();
  </code></pre>
  
  <p><code>r</code> will be a null pointer because <code>AsyncTask</code> is still downloading data.</p>
  
  <p>I've try using this code in the <code>getResponse</code> function, but it doesn't work:</p>
  
  <pre><code>public RestResponse getResponse() throws InterruptedException {
          while (getStatus() != AsyncTask.Status.FINISHED);
          return response;
      }
  </code></pre>
  
  <p>I thought that with the while loop the thread will wait until the <code>AsyncTask</code> finishes, but what I achieved was an infinite loop. </p>
  
  <p>So my question is, how could I wait until <code>AsyncTask</code> finishes so the <code>getResponse</code> method will return the correct result?</p>
  
  <p>The best solution is use of the <code>onPostExecute</code> method, but because <code>AsyncTask</code> is used by many activities I have no clue what to do.</p>