0

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.

ifconfig
  • 6,242
  • 7
  • 41
  • 65

1 Answers1

2

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.

rogcg
  • 10,451
  • 20
  • 91
  • 133
  • With your suggestion, the progress dialog now appears, but late, seemingly after doInBackground executes (I'm doing an HTTP GET and can see the GET arrive at my server seconds before the dialog shows). I had to insert a 2 second sleep within doInBackground to see the dialog before onPostExecute dismisses it. I also renamed the constructor to load_contact_list, same name as my AsyncTask. I'm on API 19 - any ideas? Thanks for your help. – ifconfig Jul 07 '14 at 04:12
  • Hi @AndroidCoder101001 first of all, **if the question solved your problem of the `ProgressDialog` not showing, please set it as the correct answer =)**. Second, I don't get exactly what you want to do, you say the `ProgressDialog` shows too fast that you can't even see it? – rogcg Jul 07 '14 at 14:01
  • " Second, I don't get exactly what you want to do, you say the ProgressDialog shows too fast that you can't even see it? " **Precisely**. – ifconfig Jul 08 '14 at 04:34
  • @AndroidCoder101001 that's totally normal, if your request doesn't late too much why do you want to force it to late? HHAHA However if you want to show the progress for the user even if the request is done(which doesn't make sense), just put a sleep with 5 seconds, if that's your intention. – rogcg Jul 08 '14 at 17:33
  • @AndroidCoder101001 you understood that? – rogcg Jul 09 '14 at 02:10