android - AlertDialog not showing while cancelling AsyncTask -


i have got little problem while cancellling asynctask, in a fragment processes data. if internet session expired asynctask should cancelled , dialog shown inform user.

however if cancel asynctask alertdialog not shown, noticed oncancelled()is not being called onpostexecute()is still executed.

if pls assist?

edit: if use while method, alertdialog shown how cancel asynctask code of tryin doinbackground() still being executed?

while (!iscancelled()) { // stuff } 

edit 2: solved! seemed asynctask call not correctly instinciated, below code works , oncancelled method called , onpostexecute declined should be. cudos anudeep bulla point me in right direction.

public class tb3_abonnement extends fragment {      private asynctask<void, void, void> task; ... @override // if fragment visible user, start asynctask     public void setuservisiblehint(boolean isvisibletouser) {         super.setuservisiblehint(isvisibletouser);          if (isvisibletouser) {                 // execute asynctask                 startfetch();         }     } // wrap (asynctask call) in own method public void startfetch() {          task = new fetchfacturen();         task.execute();     }  private class fetchfacturen extends asynctask<void, void, void> { // create new alertdialog         alertdialog taskdialog = new alertdialog.builder(getactivity())                 .setcancelable(false)                 .settitle("mededeling facturen:")                 .setmessage("uw sessie verlopen! u dient zich weer opnieuw in te loggen. ")                 .setpositivebutton(android.r.string.yes, new dialoginterface.onclicklistener() {                     public void onclick(dialoginterface dialog, int which) {                         // start loginactivity                         intent mainintent = new intent(getactivity(), loginactivity.class);                         getactivity().startactivity(mainintent);                         getactivity().finish();                     }                 })                 .seticon(android.r.drawable.ic_dialog_info)                 .create();          @override         protected void onpreexecute() {             if (helper.minutes <= 0) {                 task.cancel(true);             } else {                 showprogress(true);             }         }          @override         protected void doinbackground(void... result) {             charsequence cs1 = "€";             if (iscancelled()) {                 util.disconnect();                 return null;             }                 try {                     // heavy stuff                         }                      }                 } catch (ioexception e) {                     e.printstacktrace();                 }             return null;         }          @override         protected void oncancelled() { //  correctly called             if(iscancelled()){                 log.e("cancelled", "tab3");                 this.taskdialog.show();              }             super.oncancelled();         }          @override         protected void onpostexecute(void result) { // skipped             if(iscancelled()){                 this.taskdialog.show();             } else {                 setupinvoiceadapter();                 showprogress(false);             }         }     } 

here important code parts:

public class tb3_abonnement extends fragment {  private fetchabbo task; ... @override // if fragment visible user, start asynctask     public void setuservisiblehint(boolean isvisibletouser) {         super.setuservisiblehint(isvisibletouser);          if (isvisibletouser) {                 // execute asynctask                 task = new fetchabbo();                 task.execute();         }  public class fetchabbo extends asynctask<void, void, void> {          @override         protected void onpreexecute() {             if (helper.minutes <= 0) {                 task.cancel(true); // cancel asynctask                 new alertdialog.builder(getactivity()) // create new alertdialog                         .setcancelable(false)                         .settitle("mededeling:")                         .setmessage("uw sessie verlopen! u dient zich weer opnieuw in te loggen. ")                         .setpositivebutton(android.r.string.yes, new dialoginterface.onclicklistener() {                             public void onclick(dialoginterface dialog, int which) {                                 // start loginactivity                             }                         })                         .seticon(android.r.drawable.ic_dialog_info)                         .show();             }         }          @override         protected void doinbackground(void... result) {                 if (!this.iscancelled()) { // executed                      log.e("fetchabbo: ", "cancelled!"); // printed                     util.disconnect();                     return null;                 }                 try {                     log.e("fetchabbo: ", "still running!");                      // heavy stuff                     }                  } catch (ioexception e) {                     e.printstacktrace();                 }              return null;         }          @override         protected void oncancelled() {             log.e("cancelled", "tab3"); // not printed, oncancelled() not called?             super.oncancelled();         }          @override         protected void onpostexecute(void result) {             setuptxtviews();             mscrollview.setvisibility(view.visible); // set scrollview visible          }     } 

i may wrong, don't seem understand logic of showing dialog in onpreexecute everytime. why not try ?

public class fetchabbo extends asynctask<void, void, void> {     alertdialog taskdialog = new alertdialog.builder(getactivity()) // create new alertdialog                                             .setcancelable(false)                                             .settitle("mededeling:")                                             .setmessage("uw sessie verlopen! u dient zich weer opnieuw in te loggen. ")                                             .setpositivebutton(android.r.string.yes, new dialoginterface.onclicklistener() {                                                   public void onclick(dialoginterface dialog, int which) {                                                        // start loginactivity                                                   }                                             })                                            .seticon(android.r.drawable.ic_dialog_info)                                            .create();     @override     protected void onpreexecute() {         if (helper.minutes <= 0) {             task.cancel(true); // cancel asynctask          }     }      @override     protected void doinbackground(void... result) {             if (!this.iscancelled()) { // executed                  log.e("fetchabbo: ", "cancelled!"); // printed                 util.disconnect();                 return null;             }             try {                 log.e("fetchabbo: ", "still running!");                  // heavy stuff                 }              } catch (ioexception e) {                 e.printstacktrace();             }          return null;     }      @override     protected void oncancelled() {         log.e("cancelled", "tab3"); // not printed, oncancelled() not called?         if(this.iscancelled()){             this.taskdialog.show();         }         super.oncancelled();     }      @override     protected void onpostexecute(void result) {         if(this.iscancelled()){             this.taskdialog.show();         } else {             setuptxtviews();             mscrollview.setvisibility(view.visible); // set scrollview visible         }      } } 

Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

Cross-Compiling Linux Kernel for Raspberry Pi - ${CCPREFIX}gcc -v does not work -

java.lang.NoClassDefFoundError When Creating New Android Project -