title: Android check if this activity is not null/destroyed
body: <p>There are a lot of answered questions in Stackoverflow about checking if activity is null from a fragment, using <code>getActivity()==null</code></p>
  
  <p>How do I check if activity is not null in the activity itself?</p>
  
  <p>My specific case is this:</p>
  
  <p>activity starts an asynctask, then activity is destroyed, then asynctask returns in onPostExecute, it invokes a method in the activity (which is registered as a listener for that task) and this method uses a reference to THIS to pass a context in a method. The context is null, though.</p>
  
  <p>EDIT: Here is some code.</p>
  
  <pre><code>public interface OnGetStuffFromServerListener {
  
      void onGetStuffSuccess();
  
  }
  
  public class SomeActivity implements OnGetStuffFromServerListener {
  
      @Override
      public whatever onCreate() {
          new GetStuffFromServer(this).execute();
      }
  
      @Override
      public void onGetStuffFromServerSuccess() {
          deleteSomeFiles(this); // NPE -&gt; How do I check if activity still exists here?
      }
  
      private void deleteSomeFiles(Context context) {
          ... 
          context.getExternalFilesDir(null).toString(); // NPE toString on a null object reference
      }
  
  }
  
  public class GetSomeStuffFromServer extends AsyncTask&lt;Void, Void, Void&gt; {
  
      private OnGetSomeStuffFromServerListener listener;
  
      public GetSomeStuffFromServer (OnGetSomeStuffFromServerListener listener) {
          this.listener = listener;
      }
  
      ...doInBackground
  
      onPostExecute() {
  
          if(listener!=null) {
              listener.onGetSomeStuffFromServerSuccess();
          }
  
      }
  
  
  }
  </code></pre>
  
  <p>Actually, if I am using getApplicationContext() instead of this, maybe I will not have a problem at all?</p>