title: How to hide a ProgressBar in AsyncTask programmatically in Android?
body: <p>I have the following static <code>AsyncTask</code> class:</p>
  
  <pre><code>public static class MyAsyncTask extends AsyncTask&lt;String, Void, String&gt; {
      @Override
      protected void onPreExecute() {
          super.onPreExecute();
          progressBar.setVisibility(View.VISIBLE);
      }
  
      @Override
      protected String doInBackground(String... params) {
          //Do heavy stuff
      }
  
      @Override
      protected void onPostExecute(String string) {
          super.onPostExecute(string);
          progressBar.setVisibility(View.GONE);
      }
  }
  </code></pre>
  
  <p>I want to show/hide the <code>progressBar</code> as you see in those two methods. The problem is that I have to make the <code>progressBar</code> static. Doing this, I get:</p>
  
  <blockquote>
    <p>Do not place Android context classes in static fields; this is a memory leak (and also breaks Instant Run)</p>
  </blockquote>
  
  <p>If I remove the static that stays in front of my <code>MyAsyncTask</code> class I get:</p>
  
  <blockquote>
    <p>This AsyncTask class should be static or leaks might occur (MainActivity.MyAsyncTask)</p>
  </blockquote>
  
  <p>How to solve this?</p>
  
  <p><strong>Edit:</strong></p>
  
  <pre><code>private void checkUser() {
      uidRef.get().addOnCompleteListener(task -&gt; {
          if (task.isSuccessful()) {
              DocumentSnapshot document = task.getResult();
              User user = document.toObject(User.class);
              if (user == null) {
                  MyAsyncTask myAsyncTask = new MyAsyncTask();
                  myAsyncTask.execute(url);
              }
          }
      });
  }
  </code></pre>