android - onPostExecute called before doInBackground completes - Async task -


i have method :

public void extractfiles() {      asynctask<void, void, boolean> extractiontask = new asynctask<void, void, boolean>() {          @override         protected void onpreexecute() {         progressdialog = new progressdialog(activity.this);         progressdialog.setcancelable(false);         progressdialog.setmessage("extracting files please wait...");         progressdialog.setprogressstyle(progressdialog.style_spinner);         progressdialog.setprogress(0);         progressdialog.show();         super.onpreexecute();         }          @override         protected boolean doinbackground(void... params) {         // todo auto-generated method stub         string xapkfilepath = xapkfilepath(activity.this);         string exportdirectory = environment.getexternalstoragedirectory().getabsolutepath() + "/android/data/"             + activity.this.getpackagename() + "/files/";         file exportdirectoryfilepath = new file(exportdirectory);         exportdirectoryfilepath.mkdirs();         ziphelper zhelper = new ziphelper();           system.out.println("in background called");         zhelper.unzip(xapkfilepath, exportdirectoryfilepath);          return true;         }          @override         protected void onpostexecute(boolean result) {         super.onpostexecute(result);         if (progressdialog != null && progressdialog.isshowing()) {             progressdialog.dismiss();             system.out.println("progress dialog dismissed");         }         if (result) {             //start intent.         }         }      };     extractiontask.execute();     }  public class ziphelper {      boolean ziperror = false;      public boolean isziperror() {     return ziperror;     }      public void setziperror(boolean ziperror) {     this.ziperror = ziperror;     }      public void unzip(string archive, file outputdir) {     try {         log.d("control", "ziphelper.unzip() - file: " + archive);         zipfile zipfile = new zipfile(archive);         (enumeration e = zipfile.entries(); e.hasmoreelements();) {         zipentry entry = (zipentry) e.nextelement();          system.out.println("output dir 1*" + outputdir);         system.out.println("entry " + entry);          unzipentry(zipfile, entry, outputdir);          }     } catch (exception e) {         log.d("control", "ziphelper.unzip() - error extracting file " + archive + ": " + e);         setziperror(true);     }     }      private void unzipentry(zipfile zipfile, zipentry entry, file outputdir) throws ioexception {     if (entry.isdirectory()) {         createdirectory(new file(outputdir, entry.getname()));         return;     }      file outputfile = new file(outputdir, entry.getname());     if (!outputfile.getparentfile().exists()) {         createdirectory(outputfile.getparentfile());         system.out.println("output file " + outputfile.getparentfile());     }      log.d("control", "ziphelper.unzipentry() - extracting: " + entry);     bufferedinputstream inputstream = new bufferedinputstream(zipfile.getinputstream(entry));     bufferedoutputstream outputstream = new bufferedoutputstream(new fileoutputstream(outputfile));     try {         ioutils.copy(inputstream, outputstream);     } catch (exception e) {         log.d("control", "ziphelper.unzipentry() - error: " + e);         setziperror(true);     } {         outputstream.close();         inputstream.close();     }     }      private void createdirectory(file dir) {     log.d("control", "ziphelper.createdir() - creating directory: " + dir.getname());     if (!dir.exists()) {         if (!dir.mkdirs()) {         throw new runtimeexception("can't create directory " + dir);         }     } else {         log.d("control", "ziphelper.createdir() - exists directory: " + dir.getname());     }     }  } 

here call method extractfiles() happening before doinbackground completed extracting files showing spinner , onpostexecute called , moves next screen.

what wrong here?

public void extractfiles() {      new thetask().execute(params); } class thetask extends asynctask<void,void,void> {       ....... } 

you should call super first in

  @override     protected void onpreexecute()        super.onpreexecute();    } 

asynctask must loaded on ui thread. asynctask onpreexecute() inovked on ui thread when asynctask loaded. after doinbackground() runs in background thread. result of doinbackground() parameter onpostexecute().

when asynchronous task executed, task goes through 4 steps:

  1. onpreexecute(), invoked on ui thread before task executed. step used setup task, instance showing progress bar in user interface.

  2. doinbackground(params...), invoked on background thread after onpreexecute() finishes executing. step used perform background computation can take long time. parameters of asynchronous task passed step. result of computation must returned step , passed last step. step can use publishprogress(progress...) publish 1 or more units of progress. these values published on ui thread, in onprogressupdate(progress...) step.

  3. onprogressupdate(progress...), invoked on ui thread after call publishprogress(progress...). timing of execution undefined. method used display form of progress in user interface while background computation still executing. instance, can used animate progress bar or show logs in text field.

  4. onpostexecute(result), invoked on ui thread after background computation finishes. result of background computation passed step parameter.

http://developer.android.com/reference/android/os/asynctask.html


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -