In the past I always did AsyncTasks as private inner classes. The tutorials I found still recommend this (see here, here, here, and here). However, this has the potential to leak the context. Now Android Studio even warns about this and recommends making the class static.

I followed this advice. Here is my code for a sample project I made that updates the UI after a long running task finishes. Since the AsyncTask is static, I pass in a weak reference to the Activity and get the particular UI view that I need to update.

public class MainActivity extends AppCompatActivity {

    protected void onCreate(Bundle savedInstanceState) {

        new MyTask(this).execute();

    private static class MyTask extends AsyncTask<Void, Void, Void> {

        private WeakReference<Context> contextReference;

        MyTask(Context context) {
            contextReference = new WeakReference<>(context);

        protected Void doInBackground(Void... params) {

            try {
                // in the future I will do other long running tasks here,
                // like updating a database. I hope it is acceptable to
                // to use sleep for now. This was an actual project that 
                // I made and tested myself. 
            } catch (InterruptedException e) {
                Log.e("TAG", e.toString());
            return null;

        protected void onPostExecute(Void result) {
            AppCompatActivity context = (AppCompatActivity) contextReference.get();
            if(context != null) {
                TextView textView = context.findViewById(R.id.textview);
                textView.setText("task finished");


The above code works fine, even though it is a pain to not just update an Activity member variable directly. Also, since I don't see any tutorials or the documentation showing AsyncTask working like this, I don't know if this is the pattern I should follow from now on.

  • \$\begingroup\$ In above code, why are using context instead of text view. Use text view and make it week reference. \$\endgroup\$ Oct 24, 2017 at 10:22
  • \$\begingroup\$ agreed. Just pass in the TextView reference. If you set it, even though the context is shut, it will not do harm. \$\endgroup\$
    – Pierre
    Aug 22, 2018 at 5:44


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.