I am writing an AsyncTask as below:
class Load extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... aurl) {
//do job seconds
//stop at here, and does not run onPostExecute
}
@Override
protected void onPostExecute(String unused) {
super.onPostExecute(unused);
wait = false;
new Load().execute();
}
}
And the other method as below:
public void click() {
new Load().execute();
while(wait) {
;
}
}
The wait is a global boolean value.
This code:
will block the UI thread while the task executes. This is as bad as just running the background task in the foreground and should cause an Application Not Responding (ANR) error. Please don’t do it.
Note that
AsyncTask.onPostExecute()will not be called if the task is cancelled. It also won’t be called ifdoInBackgroundthrows an exception. Whatever is going on indoInBackgroundmay be causing this.