0

I have this GetAllUsers(); in onCreate and I have this

private void GetAllUsers() {
  (new LoadingUsers(this)).execute();
} 

Then I have this

private class LoadingUsers extends AsyncTask < Void, Integer, String > {
  String TAG = getClass().getSimpleName();
  AlertDialog.Builder builderSingle;
  ArrayAdapter < String > arrayAdapter;@
  SuppressWarnings("unused")
  Context mContext;

  public LoadingUsers(Context context) {
    super();

    mContext = context;

  }

  protected void onPreExecute() {
    //            prgDialog.show();
    //            builderSingle = new
    Log.d(TAG + " PreExceute", "On pre Exceute......");
  }

  protected String doInBackground(Void...arg0) {

    Log.d(TAG + " DoINBackGround", "On doInBackground...");
    return null;
  }

  protected void onProgressUpdate(Integer...a) {
    super.onProgressUpdate(a);
    Log.d(TAG + " onProgressUpdate", "You are in progress update ... " + a[0]);


  }

  protected void onPostExecute(String result) {
    //            prgDialog.hide();
    Log.d(TAG + " onPostExecute", "" + result);


    MainActivity.this.pd.dismiss();
  }
}

I wanted to put a builderSingle = new AlertDialog.Builder(MainActivity.this); inside the protected void onProgressUpdate(Integer... a) { which has a AsyncHttpClient client = new AsyncHttpClient(); but unfortunately the onProgressUpdate does not get called at all. I know this because the log does not show. All other log are showing except the onProgressUpdate I have also have

@
Override
protected void onDestroy() {
  super.onDestroy();
  Log.i(TAG, "On Destroy .....");
}

@
Override
protected void onPause() {
  super.onPause();
  Log.i(TAG, "On Pause .....");
}

@
Override
protected void onRestart() {
  super.onRestart();
  Log.i(TAG, "On Restart .....");
}

@
Override
protected void onResume() {
  super.onResume();
  Log.i(TAG, "On Resume .....");
}

@
Override
protected void onStart() {
  super.onStart();
  Log.i(TAG, "On Start .....");
}

@
Override
protected void onStop() {
  super.onStop();
  Log.i(TAG, "On Stop .....");
}

OnStart and OnResume are being log as well.

Why is onProgressUpdate not being called? How to call the onProgressUpdated correctly?

Update

onPostExecute is being called as well on the onProgressUpdate is not

Brownman Revival
  • 3,620
  • 9
  • 31
  • 69

4 Answers4

3

onProgressUpdate is called on the main thread each time publishProgress is called from within doInBackground (on the background thread). This facility is provided for your convenience if you choose to use it. It's primarily useful if your task involves some kind of loop, in which case you can call publishProgress at each iteration. If your task simply invokes some other code, and all the processing happens somewhere you can't control, then the publishProgress/onProgressUpdate mechanism isn't going to be useful to you. In that case, you might decide to display an indeterminate progress bar before starting the task and then hide the indeterminate progress bar after it's completed.

j__m
  • 9,392
  • 1
  • 32
  • 56
  • actually this is what i am doing showing dialog and hiding `onPostExecute` but it is not showing at all. that is why i thought the problem was in the `onProgressUpdate` – Brownman Revival Oct 26 '16 at 05:26
  • Did the task simply end so quickly that you didn't see the dialog? – j__m Oct 26 '16 at 05:29
  • is there a way to check if the dialog box appear?i think i didnt appear because there is a long pause before the UI starts to show the activity page. activity page meaning the content of the current activity. – Brownman Revival Oct 26 '16 at 05:35
  • Make your task take longer. For testing purposes, you can do `try { Thread.sleep(1000); } catch(InterruptedException x) { }`. – j__m Oct 26 '16 at 08:18
0

Your code may need following function:

@Override                                                          
protected void onProgressUpdate(Integer a) { 
    super.onProgressUpdate(a);
}
Bugs Happen
  • 2,169
  • 4
  • 33
  • 59
HGAO
  • 1
0

It should be very simple

private class LoadingUsers extends AsyncTask< Void, Integer, String > {
    private ProgressDialog dialog;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        dialog = ProgressDialog.show(getContext(), null, "Loading users...", false);
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);
        dialog.setProgress(values[0]);
    }

    @Override
    protected String doInBackground(Void... params) {
        // Doing something in loop
        int max = 1024;
        for (int i = 0; i < max; i++) {
            int percentageCompleted = i*100/max;
            publishProgress(percentageCompleted);
        }
        return "";
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        dialog.dismiss();
    }
}

Let me know if you didn't understand what is this code.

Bugs Happen
  • 2,169
  • 4
  • 33
  • 59
0

Override the function instead of just copy past, use keyword @Override

Mohammed mansoor
  • 743
  • 3
  • 11
  • 18