Note that there are some explanatory texts on larger screens.

plurals
  1. POonPostExecute() gets called but it wont dismiss my dialog
    text
    copied!<p>I have a class which extends AsyncTask, which is intended to serve as a generic task manager class for my application. </p> <p><strong>Problem</strong>:The strange behavior is that the progress dialog shows up, but is never dismissed. </p> <ul> <li><p>I am sure that onPostExecute() gets called for every task instance, as any Log.d("","") statements fire if placed in here, even the Toast messages show up from within this method, but I am not able to dismiss the static dialog. </p></li> <li><p>I understand that AsyncTask(s) have access to UI thread at only 2 places [onPreExecute() and onPostExecute()], so I think trying to dismiss the dialog in runOnUiThread() is unnecessary. </p></li> <li><p>All calls to executeTask() are made from different onCreate() methods of different activities that need to fetch some data over network before populating some of their UI elements, and I always pass the current activity's context to the tasks. </p></li> <li><p>As I do not switch activities until after the related tasks are completed, I believe the activity context objects are still valid (am I wrong to have assumed this???) I have never found any of them to be null while debugging. </p></li> <li><p>Could this be a timing issue? I have also observed that most of the times DDMS shows all tasks get completed <strong>before</strong> the activity is displayed. If I use <code>new Handler().postDelayed(runnable_which_calls_these_tasks,10);</code> in the onCreate(), and add delaying code in foo_X(), the activities are displayed without any delay, but the dialog will just not dismiss(). </p></li> </ul> <p>I have read through quite a number of articles on this issue but am still not able to figure out exactly where am I going wrong. I do not want to define each task as <code>private inner class Task1 extends AsyncTask&lt;&gt;</code> in all of my activity classes and I would not want to (unless this is the only solution) load my application object with all activity references either as mentioned in this discussion: <a href="https://stackoverflow.com/questions/3357477/is-asynctask-really-massively-flawed-or-am-i-just-missing-something">Is AsyncTask really conceptually flawed or am I just missing something?</a>. </p> <p>I have spent a week on this and am absolutely clueless :( It would be great if someone can guide me, and let me know what am I missing.<br> Following is the class definition: [I've removed some irrelevant application specific code for clarity]</p> <pre><code>public class NetworkTask extends AsyncTask&lt;Void, Integer, Boolean&gt; { private Context UIcontext; private int operationType; private static ProgressDialog dialog; private static int taskCount; private NetworkTask(int operationType Context context){ this.UIcontext = context; this.operationType = operationType; if (taskCount++ == 0) dialog = ProgressDialog.show(context,"","Loading..."); } public static Boolean executeTask(int operationType, Context context) { return new NetworkTask(operationType, context).execute().get(); } @Override protected void onPreExecute(){ super.onPreExecute(); if (taskCount == 1) dialog.show(); } @Override protected Boolean doInBackground(Void... arg0) { switch(operationType){ case TYPE_1: foo1(); break; case TYPE_2: foo2(); break; case TYPE_3: foo3(); break; case TYPE_4: foo4(); break; } @Override protected void onPostExecute(Boolean result) { super.onPostExecute(result); taskCount--; if (dialog.isShowing() &amp;&amp; taskCount == 0){ dialog.dismiss(); }else { Toast.makeText(UIcontext, "Task#"+ operationType+", m done, but there are "+taskCount+" more", 5).show(); } } </code></pre> <p>}</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload