My AsyncTask
look like:
private class MyTask extends AsyncTask<String, Void, List<Data>> {
private volatile UserData userData;
protected List<Data> doInBackground(String... params) {
// do some job 1
publishProgress();
// wait until the progress finish to update variable.
if(userData!= null){
//do some job 2
}
//do some job 3
}
protected void onProgressUpdate(Void... values) {
// try to ask user and collect information
userData= getData();
}
}
The problem is when I call publishProgress()
, it still execute job 3 in doInBackground
. How can I wait onProgressUpdate complete before continue?
Update:
In the getData() method, I tried to get the current location of user, which can't work in doInBackground and some other information from the view. I have to do this cause the job 1 take a very long time, and the current location of user can be changed. I need the most exactly location of user.
use a flag _publishFinished = false; then call publish progress then do busy waiting in doInBackgrnd
at end of onProgressUpdate call
pDialog is your progress dialog. initialize from anywhere of your activity like this:
This will might help you.
I think, the way you are using doInBackground and publishProgress() is something wrong, but still I need to see the code of method getData() .
doInBackground method should be used to do some task, which should be executed in other thread than event thread, and in publishProgress method, you should execute tasks which works with event thread. So, if getData() has nothing to do with event thread, please write the code in doInBackground itself.
However, if your requirements are just use both these methods, as present, then you can add a synchronize block, as like below: