title: publishing progress in shared class in doBackgorund in AsyncTask
body: <p>I have several different <code>AsyncTasks</code> that need to perform the same shared basic operation and show progress while doing so. Rather than copying a lot of code several times (with progress showing in each) is there a way to share the code by passing in the other class? The <code>AsyncTasks</code> are very different and you can't just pass in the Abstract, which has a protected <code>publishProgress()</code> method anyway. Looked at a bunch of examples and tried to a lot of different variations without success.</p>
  
  <p>So here are the details...lets say we have two different tasks doing different stuff, but sharing the need to hit a server, download a number of files and then put them in a DB (and then do other stuff):</p>
  
  <pre><code>public class ExampleTaskFragment extends ListFragment {
  
      private ProgressDialog mProgress;
  
      private void hideProgress() {
          mProgress.dismiss();
      }
  
      private void showProgress(String message) {
          mProgress = ProgressDialog.show(getActivity(), null, message, true, false);
      }
  
      protected void updateProgressMessage(String message) {
          mProgress.setMessage(message);
      }
  
      public class TaskA extends AsyncTask&lt;Object, String, ArrayList&lt;A&gt;&gt; {
  
          public void progressUpdate(String... values) {
               publishProgress(values);
          }
  
          @Override
           protected void onCancelled() {
                hideProgress();
           }
           @Override
           protected void onPreExecute() {
               showProgress("Stuff before shared...");
           }
           @Override
           protected ArrayList&lt;A&gt; doInBackground(Object... params) {
  
              // DoStuffPublish is the common stuff both tasks share
              DoStuffPublish dostuff = new DoStuffPublish(this);
              return doMoreStuffA();
           }
  
           @Override
           protected void onPostExecute(ArrayList&lt;A&gt; result) {
               updateProgressMessage("After shared stuff...");
               hideProgress();
           }  
  
           ArrayList&lt;A&gt; doMoreStuffA() {
              return new ArrayList&lt;A&gt;();
           }
      }
  
      public class TaskB extends AsyncTask&lt;Object, String, ArrayList&lt;B&gt;&gt; {
  
          public void progressUpdate(String... values) {
               publishProgress(values);
          }
  
          @Override
           protected void onCancelled() {
                hideProgress();
           }
           @Override
           protected void onPreExecute() {
               showProgress("Stuff before shared...");
           }
           @Override
           protected ArrayList&lt;B&gt; doInBackground(Object... params) {
  
              // DoStuffPublish is the common stuff both tasks share
              DoStuffPublish dostuff = new DoStuffPublish(this);
              return doMoreStuffB();
           }
  
           @Override
           protected void onPostExecute(ArrayList&lt;B&gt; result) {
               updateProgressMessage("After shared stuff...");
               hideProgress();
           }  
  
           ArrayList&lt;B&gt; doMoreStuffB() {
              return new ArrayList&lt;B&gt;();
           }
      }
  }
  
  public class DoStuffPublish {
  
      private AsyncTask task;
  
      public DoStuffPublish(AsyncTask task) {
          super();
          this.task = task;
          doSharedStuff();
      }
  
      protected void publishProgress(String message) {
          if (task instanceof TaskA) {
              ((TaskA)task).progressUpdate(message);
          }
          else if (task instanceof TaskB) {
              ((TaskB)task).progressUpdate(message);
          }
      }
  
      public  void doSharedStuff() {
  
          publishProgress("Doing Shared stuff...");
  
          // Do lots of shared things here...
  
          publishProgress("Done doing shared stuff...");
  
       }
  }
  </code></pre>
  
  <p>This code has been cobbled together - so don't get too hung up on the syntax. The concept it there...I want to have a shared class I can call in 2 or more tasks that can share that code and still publish updates. I have tried putting the ProgressDialog in the tasks and outside without success. When I have done so I get "Only the original thread that created a view hierarchy can touch it's views". And in the debugger you can see the two different threads - the original main UI thread and the thread created in the  Form doInBackground(Object... params).</p>
  
  <p>I fear this may not be possible...</p>