android - onPostExecute() gets called but it wont dismiss my dialog -


i have class extends asynctask, intended serve generic task manager class application.

problem:the strange behavior progress dialog shows up, never dismissed.

  • i sure onpostexecute() gets called every task instance, log.d("","") statements fire if placed in here, toast messages show within method, not able dismiss static dialog.

  • i understand asynctask(s) have access ui thread @ 2 places [onpreexecute() , onpostexecute()], think trying dismiss dialog in runonuithread() unnecessary.

  • all calls executetask() made different oncreate() methods of different activities need fetch data on network before populating of ui elements, , pass current activity's context tasks.

  • as not switch activities until after related tasks completed, believe activity context objects still valid (am wrong have assumed this???) have never found of them null while debugging.

  • could timing issue? have observed of times ddms shows tasks completed before activity displayed. if use new handler().postdelayed(runnable_which_calls_these_tasks,10); in oncreate(), , add delaying code in foo_x(), activities displayed without delay, dialog not dismiss().

i have read through quite number of articles on issue still not able figure out going wrong. not want define each task private inner class task1 extends asynctask<> in of activity classes , not want (unless solution) load application object activity references either mentioned in discussion: is asynctask conceptually flawed or missing something?.

i have spent week on , absolutely clueless :( great if can guide me, , let me know missing.
following class definition: [i've removed irrelevant application specific code clarity]

public class networktask extends asynctask<void, integer, boolean> {    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() && taskcount == 0){             dialog.dismiss();     }else {         toast.maketext(uicontext, "task#"+ operationtype+", m done, there "+taskcount+" more", 5).show();     } } 

}

edited:

mystery solved - 1 giving me bit of trouble. there few problems:

  1. the main problem why dialog not showing networktask.get(). blocking call waits whole networktask finish. during time blocks ui thread nothing drawn (also other ui elements unresponsive). remove get():

    public static boolean executetask(int operationtype, context context){     new networktask(operationtype, context).execute();     return true; // return whatever }  
  2. the show() on progressdialog called twice. shows 2 dialogs, 1 after another. remove show() inside onpreexecute().

  3. progressdialog modal - prevents changing ui until done. toast.maketext() called before dialog.dismiss() since dialog blocking drawing screen, queued , shown after dialog dismissed.

  4. super.onpostexecute(result) , super.onpreexecute() redundant can removed.

i can post whole working code if have trouble it.


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

aspxgridview - Devexpress grid - header filter does not work if column is initially hidden -

c# - How to execute a particular part of code asynchronously in a class -