Android Change UI From An AsyncTask

To change the UI from a Asynctask in Android Java the easiest way is to pass a Context into the constructor of the AsyncTask.

public class myTask extends AsyncTask<URL, Integer, Long> {
   Context mContext;
   myTask(Context context) {
   this.mContext = context;
   }

   protected Long doInBackground(URL... urls) {
      // code 
   }

   protected void onPostExecute(Long result) {
      // code
   }
}

Now you have access to the context and can change the UI in onPostExecute

Posted in Android, Tips and Tricks Tagged with: , ,