I'm trying to implement a progress bar into my application which is shown while my AsyncTask
is fetching data.
For this I tried to rebuild this SO-Answer ("SO: How to show a progress spinner").
But my progress bar is shown up (and instantly dismissed) on the end of my AsyncTask
when all the work is already done. "Peter" ask a similar answer "here" more than a year ago but didn't get a heling answer.
This is my AsyncTask
. Info: AppActivity
is the parent class of all the activities in my application. Some contain fragments, some not. Some use my RESTTask
.
public class RESTTask extends AsyncTask<Void, Void, String> {
protected AppActivity AppActivity;
/*
* Special constructor to pass the activity
*/
public RESTTask(AppActivity activity) {
AppActivity = activity;
}
@Override
protected String doInBackground(Void... params) {
String resultstring = "";
// fetch data from a webservice
return resultstring;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
if (AppActivity != null) {
AppActivity.showDialog();
}
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if (AppActivity != null) {
AppActivity.dismissDialog();
}
}
}
My AppActivity
:
public class AppActivity extends Activity {
public ProgressDialog dialog;
@Override
protected void onResume() {
super.onResume();
dialog = new ProgressDialog(this);
}
public void showDialog() {
this.dialog.setMessage(getString(R.string.Please_wait));
this.dialog.show();
}
public void dismissDialog() {
if (this.dialog.isShowing()) {
this.dialog.dismiss();
}
}
}
So my question is now: How to correctly implement the progress bar so it's shown up WHILE the AsyncTask
is performing and not only after it ?
updated info: I hope this example makes it clearer what does not work as expected:
In one of my activities (exact in one of the fragments of one of my activites) I want to call a webservice. The webservice-method takes about ten seconds to answer because of it's complexitiy. So I expect to see the progress bar to be shown from the moment the webservice is called until the answer is recieved. But what I can see while debuging is the following:
- The
onPreExecute
is called and the dialog should be shown, but the emulator only shows a white screen - The
doInBackground
is called. In eclipse'LogCat
I see that the webservice is called and the result is dealt with. Screen is still. When calling the webservice the emulator stills shows a white screen. - The
onPostExecute
is called to dismiss the dialog. At the beginning of theonPostExecute
the final rendered screen is shown. - After resuming from the
onPostExecute
the dialog flashs up and is closed instantly after that.
Hope this helps to understand the problem.
edit: Updated source code after fixed the error with the leak window
-exception. Also updated question for hopefully make it clearer.
[2]: Activity has leaked window that was originally added "SO: Leaked window exception