0

I am trying to close progress dialog after exception occurred, but my current scenario is not allowing me to.

I have 3 classes:

  1. MainActivity
  2. SecondClass
  3. ThirdClass

From MainActivity I am running a task using AsyncTask and shows a progress dialog, while a task is done in background.

@Override
public void onCreate(Bundle savedInstanceState) 
{

 // my code ...

 BackgroundTask bt = new BackgroundTask();
 bt.execute();

}
private class BackgroundTask extends AsyncTask<Void, Void, Void> 
{
   private ProgressDialog PD;

   @Override
   protected void onPreExecute() 
   {
      PD =  new ProgressDialog(getActivity());
      PD.setMessage("Performing background tasks...");
      PD.setCancelable(false);
      PD.show();
   }

   @Override
   protected Void doInBackground(Void... arg0) 
   {
      // call SecondClass's getMyPhotos method to perform some tasks
      // ...

      return null;
   }

   protected void onPostExecute(Void... arg0)
   {
      Log.i("MainActivity", "--called--");
   }
}

My SecondClass is simple Java class with some methods

public class SecondClass
{
   // ....
   public void getMyPhotos()
   {
      // from here I call ThirdClass's internetRelatedStuff method
   }
}

And lastly in ThirdClass I am doing some internet related stuff in AsyncTask:

public class ThirdClass
{
   // ....
   public void internetRelatedStuff()
   {
      try
      {
         // again some other stuff...

         // LoadImage asyncTask        
         LoadImage loadImg = new LoadImage();
         loadImg.execute();
      }
      catch(Exception e)
      {
          if(e.getErrorCode() == 34)
          {
             // if I get an exception here, how am I suppose to close close Progress dialog
             // of MainActivity
          }
      }
   }    

private class LoadImage extends AsyncTask<.......> 
{
 // .....
}   

}

So in my ThirdClass's try/catch, is it possible if an exception occurred, I can close the Progress dialog which is showing from MainActivity?

pro_newbie
  • 336
  • 2
  • 6
  • 19

2 Answers2

0

Yes, there are multiple ways to do this. you can pass the ProgressDialog object in the MainActivity to the SecondClass's getMyPhotos() method then to the ThirdClass constructor then dismiss the dialog

Abdallah Alaraby
  • 2,222
  • 2
  • 18
  • 30
  • Could you please tell me any other of doing that, beside passing ProgressDialog object to other class. – pro_newbie Sep 23 '14 at 08:37
  • You can declare the `ProgressDialog` to be static and access it from anywhere, but note that static variables are not recommended in Android. OR You can make the first `AsyncTask` to wait for the execution of the second `AsyncTask` by using `loadImg.get()` instead of `loadImg.execute()` – Abdallah Alaraby Sep 23 '14 at 10:03
  • I have tried declaring ProgressDialog as static, but when from ThirdCalss I called MainActivity's method to close dialog then PD is null, so PD.dismiss() is not called. `if(PD!=null && PD.isShowing()) { PD.dismiss(); }` – pro_newbie Sep 23 '14 at 11:52
  • [This answer](http://stackoverflow.com/a/17900913/2185634) explains why static fields are reinitialised. Try using the first or third solutions that i suggested. both of them should work without problems. – Abdallah Alaraby Sep 23 '14 at 11:58
  • Could you please elaborate `loadImg.get()` solution. Because I have my first AsyncTask in MainActivity and my second AsyncTask is in ThirdClass, so how am I suppose to wait for second AsyncTask to finish in first AsyncTask? – pro_newbie Sep 24 '14 at 09:04
  • The current code tells that the second `AsyncTask` is running inside the first `AsyncTask`, So all you have to do is call the `loadImg.get()` and return the result back from `internetRelatedStuff` to the first `AsyncTask`. – Abdallah Alaraby Sep 24 '14 at 09:40
0

There are couple of ways you can do this, let me point out two ....

  1. You can pass your dialog from activity to SecondClass and then from SecondClass to third class, and use it there to close the dialog ... You will have define ProgressDialog in both second and thirdclass,

  2. Create a static variable ProgressDialog in Activity and a static function closeDialogs() where you can close your dialogs and you can call it using static ref. from third class, I personally like this one ....

maaz
  • 204
  • 3
  • 5
  • I have tried the second method. In ThiredClass I have done: `MainActivity.closeDialog();` and in MainActivity I declared ProgressDialog as **public static ProgressDialog PD;** and created a static method: `public static void closeDialog()` `{` `if(PD.isShowing())` `{` `PD.dismiss();` `}` `}` but I got a NullPointerException at **PD.isShowing()** – pro_newbie Sep 23 '14 at 08:40
  • `private class BackgroundTask extends AsyncTask` `{ private ProgressDialog PD; // You removed this declaration right ...` – maaz Sep 23 '14 at 09:43
  • Yes, I have removed it and I have declared `public static ProgressDialog PD` outside of onCreate method, and used PD in Async Task. – pro_newbie Sep 23 '14 at 09:56
  • Put a check for PD being null and see how it behaves ... `if(PD!=null && PD.isShowing())` – maaz Sep 23 '14 at 10:09
  • `if(PD!=null && PD.isShowing()) { PD.dismiss(); }` tried this too, didn't get the NullPointerException, but the code in if statement is not called. Still showing the Progress dialog. – pro_newbie Sep 23 '14 at 10:14