Issue
I know there are a million similar questions here, but none of the solutions presented there quite addressed the issue I'm having. I have an AsyncTask
that loads data from a database. An indeterminate ProgressDialog
is displayed while the data is loading (even though in production code it never takes a visibly apparent time to load the database, but just in case someone builds a huge one).
I used to call and dismiss the ProgressDialog
directly from the AsyncTask
, and it worked fine. The problem is, I'm trying to enforce better separation of concerns and I decided that displaying things to the screen does not fall in the AsyncTask
's domain, and really belongs to the activity. So I implemented a callback interface to the activity, and in the activity I have:
private ProgressDialog progressDialog;
@Override
public void onPreExecute() {
progressDialog = ProgressDialog.show(this,"","Loading. . .",true,true);
}
@Override
public void onPostExecute() {
if (progressDialog != null) {
Logger.d("Dismissing progress dialog");
progressDialog.dismiss();
progressDialog = null;
}
}
Logcat shows that I'm reaching that logger line, but the ProgressDialog
never dismisses. Is there some reason why I can't do this in a callback?
Solution
Most likely what's happening here is that multiple ProgressDialog
's are being instantiated.
Therefore, while dismiss()
is successfully called, it's not called on the ProgressDialog
currently visible on your screen, and you've lost your reference to this visible one since you've overwritten progressDialog
with a new object instance.
Try putting Logger.d("New ProgressDialog");
where you are creating a new instance of the ProgressDialog
, and you will most likely see this scenario confirmed.
You could probably fix this by using an Array
/List
to keep track of all of your ProgressDialog
s and dismiss the appropriate one when it's corresponding AsyncTask
completes, but personally I think this is far more convoluted then your original approach of having each AsyncTask
handle their own ProgressDialog
's lifecycle.
Answered By - Martin Konecny
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.