sudo programmer.sh sudo programmer.sh -4 years ago 58
Android Question

ProgressDialog not working properly in an AsyncTask

While trying to impliment How to show ProgressDialog across launching a new Activity? via @Slartibartfast's answer, I tried unsuccessfully to get it to work in my editor. Here, I am trying to display a ring progressDialog while the program fetches some contacts' information. Then, later on, in the OnCreate, it puts it in a ListView. My problem is that no progressDialog ever appears. My code is as follows:

Declaration

private ProgressDialog ringProgressDialog = null;


AsyncTask - sets off and ends the ring progressDialog

private class load_contact_list extends AsyncTask<String, Void, Integer> {

@Override
protected Integer doInBackground(String... url) {

...
}

@Override
protected void onPostExecute(Integer list_length) {
ringProgressDialog.dismiss();
setContentView(R.layout.activity_main);
}
}


OnCreate

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

ringProgressDialog = ProgressDialog.show(MainActivity.this, "Loading contacts", "Please Wait", true);

new load_contact_list().execute(...);
}
...


My best efforts to make my code like his has proved in vein, I don't know why it isn't working. Thanks in advance.

EDIT: The set of ellipses in the doInBackground() is where the contact info is fetched. The other set, in the OnCreate, is where the info is put into the list.

Answer Source

Looks like the problem is when you create the ProgressDialog the way you are creating. Also it would be a better idea to start the ProgressDialog in the onPreExecute() method inside the AsyncTask class.

1. Create a constructor for your AsyncTask

Create a constructor for your AsyncTask class, this way you are able to send the context reference via the class instantiation, and declare the ProgressDialog inside your AsyncTask class.

private class load_contact_list extends AsyncTask<String, Void, Integer> 
{
    private Activity mActivity;
    private Context mContext;
    private ProgressDialog mDialog;

    // Constructor
    public ProgressTask(Activity activity) 
    {
        this.mActivity= activity;
        mContext= activity;

        // Here we create a new instance of the ProgressDialog with the context   received as parameter
        mDialog= new ProgressDialog(mContext);
    }

    protected void onPreExecute() 
    {
        // We use the ProgressDialog object instantiated in this class's constructor
        this.mDialog.setMessage("Loading contacts");
        this.mDialog.show();
    }

    @Override
    protected Integer doInBackground(String... url) 
    {
        // ...
    }

    @Override
    protected void onPostExecute(Integer list_length) 
    {
        // Here we dismiss the ProgressDialog created in this class's constructor
        if(mDialog.isShowing())
        {
            this.mDialog.dismiss();
        }
        setContentView(R.layout.activity_main);
    }
}

2. Excute the AsyncTask also sending the activity's context

Now for in your activity onCreate() method, you execute the AsyncTask, and don't forget to send the activity's context via the AsyncTask constructor.

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);

    // Execute the AsyncTask class by sending the context by parameter via its constructor
    new load_contact_list(this).execute();
}

Hope it helps you.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download