title: How to close Progress dialog from background
body: <p>I am kinda new to android and stuck in a situation, where I don't know how to handle it.</p>
  
  <p>Here is what I am trying to do:</p>
  
  <p>From Activity I start some tasks and show Progress-Dialog  while the task completes in background.
  If the task successfully completes I close the Activity and start new Activity.</p>
  
  <p>But if in background I caught an exception I want to go back to the activity and close the Progress-Dialog and show a Toast maybe that some exception has occurred.</p>
  
  <p>From Activity:</p>
  
  <pre><code>@Override
  public void onCreate(Bundle savedInstanceState) 
  {
   // my code
  
   // start Progress Dialog
     showProgressDialog();  
  }
  
  public void showProgressDialog()
  {
     PD =  new ProgressDialog(getActivity());
     PD.setMessage("Downloading.....");
     PD.show();
  }
  
  @Override
  public void onDestroy()
  {
     super.onDestroy();
     if (PD != null) 
     {
        PD.dismiss();
        PD = null;
     }
  }
  </code></pre>
  
  <p>and in background:</p>
  
  <pre><code>try 
  {
     // perform some db tasks 
     // using AsyncTask 
  }
  catch (Exception e) 
  {
     if(e.getErrorCode() == 34)
     {
        // From here I want go back to Activity and close the Progress dialog
        // and shows what error has occurred.
     }
  }
  </code></pre>
  
  <p>So if exception occurred how I am suppose to go back to the activity and close that Progress-dialog, or is there any other way to doing this?</p>