Activity is destroyed but asynctask is still running
<p>I am working on push notifications. The issue is when i am loading a form which is very huge and i recieve notification, before the form is completely loaded i try to click on the notification i get a blank screen after 5-6 seconds. The exception is due to dialog.dismiss.  </p>

<p>According to my understanding, while opening notification the current activity is destroyed a new activity is created. while creating a new activity i am using asynctask to accomplish some other functionality in my app. So when the current activity is destroyed, activity context is null but the asynctask is still running also progress dialog. As soon as the activity is destroyed there is no window to show the dialog and hence window leaked exception.  </p>

<p>Can anybody help me to get me out of this issue.I also get the blank screen when app is idle for a long time then i open the notifications.  </p>

<p>Is there a way to stop running the asynctask as soon as the activity is destroyed.  </p>

<p>My code is :  </p>

<p>public class MainActivity extends Activity {  </p>

<pre><code>  private MyProgressDialog myProgressDialog;  
  public  LinearLayout mainPanel;  
  private VMobilet mobilet = null;  
  private String mobiletId;  
  private String formId ;

  @Override  
protected void onCreate(Bundle icicle) {  
    super.onCreate(icicle);   
    setContentView(mainPanel);   

    Intent i = getIntent();  
    mobiletId = i.getStringExtra("Mobilet Id");  
    formId = i.getStringExtra("Form ID");  
            VUiHelper.getInstance().setIsFinish(false);  
    myProgressDialog = MyProgressDialog.show(MainActivity.this,"","",true);  

    BackgroundTask backgroundTask = new BackgroundTask();  
    backgroundTask.execute(MainActivity.this);  
}  

    @Override  
protected void onPause() {  
    super.onPause();  
    if(VUiHelper.getInstance().isFinish())  
        {         
            this.finish();    
        }       
    else {  
         System.out.println("pausing mainactivity");  
    }  
}  

    @Override  
protected void onDestroy() {  
    super.onDestroy();  
    System.out.println("mainactivity ondestroy called");  
    if(mobilet != null)  
    mobilet.getForms().clear();  
    mobilet = null;  
    mainPanel = null;  
    VUiHelper.getInstance().clearControlCache();  
    VUiHelper.getInstance().MediaInput.clear();  
    System.gc();  
}  

    private class BackgroundTask extends AsyncTask&lt;Context, String, Boolean&gt; {  

    @Override  
    public void onPreExecute() {    
        super.onPreExecute();   
    }  

    @Override  
    protected Boolean doInBackground(Context... arg0) {  
        if (mobiletId != null) {  
            ** some logic **  
        }  
        return true;  
    }  

    @Override  
    public void onPostExecute(Boolean status){              
         super.onPostExecute(status);          
         myProgressDialog.dismiss();  //dismissing the progress dialogs    
         if (mobilet != null) {  
                      ** some logic  **  
         } else {  
              ** some logic **  
                 }  
     }  
     }      
}  
 }  
</code></pre>

<p>I tried printing the activity context in onDestroy() and it is null.</p>
