java - onPostExecute() invoked before doInBackground() method in the case of AsyncTask -
consider code below:
package com.reallybelievebig.asynctaskextracredit; import android.os.asynctask; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.util.log; import android.view.menu; import android.view.menuitem; import android.view.view; import android.widget.textview; import android.widget.toast; public class asynctasktestactivity extends appcompatactivity { private static final string tag = asynctasktestactivity.class.getsimplename(); protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_async_task_test); new testtask().execute("execute"); log.d(tag, "in ui thread!"); } private class testtask extends asynctask<string, integer, string> { @override protected string doinbackground(string... params) { (int = 0; <= 100; i+=20) { try { thread.sleep(50); } catch (interruptedexception e) { log.e(tag, "exception caught: ", e); } publishprogress(i); } return "background task complete"; } @override protected void onprogressupdate(integer... values) { toast.maketext(asynctasktestactivity.this, "progress: " + values[0], toast.length_long).show(); } @override protected void onpostexecute(string s) { super.onpostexecute(s); log.d(tag, "in onpostexecute: " + s); toast.maketext(asynctasktestactivity.this, s, toast.length_long).show(); } } }
i starting asynchronous task main ui thread using
testtask.execute()
method.the
doinbackground()
method working fine. sleeps time, invokespublishprogress()
method , displays toast inonprogressupdate()
method.i expecting
onpostexecute()
method called afterdoinbackground()
completed. start application, log message inonpostexecute()
method.
there wrong not able figure out. issue in code?
Comments
Post a Comment