I am trying to close progress dialog after exception occurred, but my current scenario is not allowing me to.
I have 3 classes:
- MainActivity
- SecondClass
- 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?