3

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:

  1. The onPreExecute is called and the dialog should be shown, but the emulator only shows a white screen
  2. 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.
  3. The onPostExecute is called to dismiss the dialog. At the beginning of the onPostExecute the final rendered screen is shown.
  4. 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

Community
  • 1
  • 1
bish
  • 3,381
  • 9
  • 48
  • 69
  • Check this post: http://stackoverflow.com/questions/3821423/background-task-progress-dialog-orientation-change-is-there-any-100-working. It worked very well for me. – Szymon Sep 12 '13 at 12:26
  • 1
    You have not return an string in `doInBackground` method and You can't handle UI in `doInBackground`. – Sunny Sep 12 '13 at 12:29
  • @st. Checked it and it couldn't help me :/ – bish Sep 12 '13 at 13:49
  • Updated my question after the `leaked window` problem was solved and add some info about my problem to make it clearer. – bish Sep 12 '13 at 13:50

2 Answers2

2

1. Your onPostExecute() is Never called

Because you returns String from protected String doInBackground(Void... params)

And you have protected void onPostExecute(final Boolean success)

So Change to

 @Override
 protected void onPostExecute(String result) {
    super.onPostExecute(result);
    if (this.dialog.isShowing()) {
       this.dialog.dismiss();
      }  
  }

2. you can't Handle/update UI part in doInBackground()

So remove following from doInBackground()

if (this.dialog.isShowing()) {
      this.dialog.dismiss();
    }  
Tarsem Singh
  • 14,139
  • 7
  • 51
  • 71
  • Thanks for the notice about the `onPostExecute`. I changed this so one of the problems (the `leaked windows`-exception) has gone. But the progressbar still pops up and instantly disappears at the end of the `AsynchTask` work. Do you have any idea for this either? – bish Sep 12 '13 at 13:15
1

display your dialog in onPreExecute()

try this code

class MyTask extends AsyncTask<Void, String, String> {

    @Override
    protected void onPreExecute() {

        super.onPreExecute();
        //display dialog here
        dialog.show();

    }
    @Override
    protected String doInBackground(Void... params) {
        //do your stuff here
        return myString;
    }

    @Override
    protected void onPostExecute(String result) {

        super.onPostExecute(result);
        //dismiss dialog here
        dialog.dismiss();
    }

}
Ketan Ahir
  • 6,678
  • 1
  • 23
  • 45