title: Android Development: Having an AsyncTask in a separate class file
body: <p>I have been playing around with various examples trying to familiarize myself with AsyncTask. So far all the examples I have seen have had the AsyncTask included into the onCreate method of the main activity. Which I don't like very much, so I was wanting to see how hard it would be to separate it into its own class. So far I have this:</p>
  
  <p>the <strong>main activity</strong></p>
  
  <pre><code>package com.example.asynctaskactivity;
  
  import android.os.AsyncTask;
  import android.os.Bundle;
  import android.os.SystemClock;
  import android.app.Activity;
  import android.util.Log;
  import android.view.View;
  import android.widget.Button;
  import android.widget.ProgressBar;
  import android.widget.TextView;
  import android.widget.Toast;
  
  import com.example.asynctaskactivity.ShowDialogAsyncTask;
  
  public class AsyncTaskActivity extends Activity {
  
   Button btn_start;
   ProgressBar progressBar;
   TextView txt_percentage;
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
  
          btn_start = (Button) findViewById(R.id.btn_start);
          progressBar =  (ProgressBar) findViewById(R.id.progress);
          txt_percentage= (TextView) findViewById(R.id.txt_percentage);
          Log.v("onCreate","Attempt set up button OnClickListener");
          btn_start.setOnClickListener(new View.OnClickListener() 
          { 
             @Override
             public void onClick(View v) {
                 btn_start.setEnabled(false);
                 new ShowDialogAsyncTask().execute();
             }
          });
  
          Log.v("onCreate","Success!"); 
      }
  }
  </code></pre>
  
  <p>the new seperate <strong>AsyncTask class</strong></p>
  
  <pre><code>package com.example.asynctaskactivity;
  
  import android.os.AsyncTask;
  import android.os.SystemClock;
  import android.util.Log;
  import android.widget.Button;
  import android.widget.ProgressBar;
  import android.widget.TextView;
  import android.widget.Toast;
  
  public class ShowDialogAsyncTask extends AsyncTask&lt;Void, Integer, Void&gt;{
  int progress_status;
  
  @Override
      protected void onPreExecute() {
      // update the UI immediately after the task is executed
      Log.v("onPreExecute","1");
      super.onPreExecute();
      Log.v("onPreExecute","2");
      //Toast.makeText(AsyncTaskActivity.this,"Invoke onPreExecute()", Toast.LENGTH_SHORT).show();
      progress_status = 0;
      Log.v("onPreExecute","3");
      txt_percentage.setText("downloading 0%");  
      Log.v("onPreExecute","4");
      }
  
      @Override
      protected Void doInBackground(Void... params) {
          Log.v("doInBackground","1");
          while(progress_status&lt;100){
  
              progress_status += 2;
  
              publishProgress(progress_status);
              SystemClock.sleep(300);
  
          }
          return null;
      }
  
      @Override
      protected void onProgressUpdate(Integer... values) {
      super.onProgressUpdate(values);
  
      progressBar.setProgress(values[0]);
      txt_percentage.setText("downloading " +values[0]+"%");   
      }
  
      @Override
      protected void onPostExecute(Void result) {
      super.onPostExecute(result);
  
      //Toast.makeText(AsyncTaskActivity.this,"Invoke onPostExecute()", Toast.LENGTH_SHORT).show();
  
      txt_percentage.setText("download complete");
      btn_start.setEnabled(true);
      }
  }
  </code></pre>
  
  <p>Originally this was all in the main activity, hence the mentions to the elements that the asynctask should in theory update. Obviously at present this is causing runtime errors, which then got me thinking. How can I have the file seperate but still update the UI thread.</p>
  
  <p>Sorry if this is a stupid question, quite new to android development and background threads in particular.</p>