0

I want to use a ProgressDialog within a Fragment for an AsyncTask. With Activity the following code works perfectly, but with Fragment no.

public class ResultTask extends AsyncTask<String, Void, Report> {

    private ProgressDialog pDialog;
    private Context context;

    public ResultTask(Context context) {
        super();
        this.context = context;

    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(context);
        pDialog.setMessage("Loading...");

        pDialog.show();
    }

    @Override
    protected Report doInBackground(String... params) {
      //myLong operation

    }

    @Override
    protected void onPostExecute(Report report) {
        pDialog.dismiss();
        super.onPostExecute(report);
    }

}

How Can I fix in a very simple way ? The idea of the ProgressBar, is just to let understand the user, that a long operation is processed.

UPDATE

Here is the code of the Fragment, where I execute the AsyncTask:

ResultTask task = new ResultTask(getActivity());
Report report = task.execute("paramenters").get();

And this is the code of the Activity, where I select the Fragment:

private void changeActivity(Button button, final Fragment fragment) {
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                FragmentManager fragmentManager = getFragmentManager();
                fragmentManager.beginTransaction()
                        .replace(R.id.container, fragment).commit();
            }
        });

    }
I love coding
  • 1,183
  • 3
  • 18
  • 47

1 Answers1

1

If you use .get() for an AsyncTask than it is going to WAIT there until the result not arriving, which case the UI thread to be blokced, while task is running. USE .execute and use handlers to communicate with the task. In this case, you don`t need a handler, because the onPre and onPost methods are related the UI, but the doInBackGround not :)

http://developer.android.com/reference/android/os/AsyncTask.html#get()

article: http://www.vogella.com/tutorials/AndroidBackgroundProcessing/article.html

narancs
  • 5,234
  • 4
  • 41
  • 60
  • 1
    What kind of handler should I use ? – I love coding Jun 19 '15 at 09:35
  • In your case you don`t need handler, because you are just dismissing the dialog, so it is good to use the onPostExecute for this. – narancs Jun 19 '15 at 09:36
  • So to let display correctly the progress dialog, I need to create a method in the fragment, which will be called in the onPostExecute() method. isn't it ? In this case, the AsyncTask will not return directly the object Report. isn't it ? – I love coding Jun 19 '15 at 10:36
  • No, just pass the dialog to asynctask constructor. On asynctask consturctor save it as instance reference. after it use this reference in onPreExecute and say: this.dialog.show(); – narancs Jun 19 '15 at 10:41
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/80997/discussion-between-i-love-coding-and-karoly). – I love coding Jun 19 '15 at 10:50
  • I have two more questions: why should I pass the getContext to the asyncTask ? Where should I dismiss the progress dialog ? Now the progress is showed once the operation has finished and I don't understand why... – I love coding Jun 19 '15 at 18:52