java - How to modify external field from onPostExecute method of an inner AsyncTask class? -
in next snippet:
public class externalclass { private int num = 1; public void backgroundtask() { new httptask().execute(); } public int getnum() { homecoming num; } private class httptask extends asynctask<string, void, string> { @override protected string doinbackground(string... params) { //do stuff... } @override protected void onpostexecute(string result) { //do stuff... externalclass.this.num = 2; } }
in activity:
... externalclass ec = new externalclass(); ec.backgroundtask(); int mynum = ec.getnum(); //num 1, not 2!!
what doing wrong? works fine, doinbackground() , onpostexecute() finish field "num" don't change. tried "num = 2" or "this.num = 2" (i know no correct, but...).
you need utilize asynctask.get() method result in main ui thread asynctask when doinbackground()
execution finish
public void backgroundtask() { new httptask().execute().get(); }
note: when u phone call get() method of asynctask in main ui thread stop exection of main thread . need phone call backgroundtask()
method background thread
java android android-asynctask
No comments:
Post a Comment