ProgressDialog dismissal in android
Asked Answered
C

2

7

I want to open a ProgressDialog when I click on the List Item that opens the data of the clicked Item form the Web Service. The ProgressDialog needs to be appeared till the WebContent of the clicked Item gets opened.

I know the code of using the Progress Dialog but I don't know how to dismiss it particularly.

I have heard that Handler is to be used for dismissing the Progress Dialog but I didn't found any worth example for using the Handler ultimately.

Can anybody please tell me how can I use the Handler to dismiss the Progress Dialog?

Thanks, david

Contreras answered 20/1, 2011 at 10:14 Comment(0)
A
15

Hi this is what you want

        public void onClick(View v)
        {
            mDialog = new ProgressDialog(Home.this);
            mDialog.setMessage("Please wait...");
            mDialog.setCancelable(false);
            mDialog.show();
            new Thread(new Runnable()
            {
                @Override
                public void run()
                {
                    statusInquiry();
                }
            }).start();
        }

here is the web webservice that is called

void statusInquiry()
{
    try
    {
        //calling webservice
                    // after then of whole web part you will send handler a msg
        mHandler.sendEmptyMessage(10);
    }
    catch (Exception e)
    {
        mHandler.sendEmptyMessage(1);
    }
}

and here goes handler code

Handler mHandler = new Handler()
{
    public void handleMessage(android.os.Message msg)
    {
        super.handleMessage(msg);

        switch (msg.what)
        {
            case 10:
                mDialog.dismiss();
                break;
                    }
             }
      }
 };
Archaeology answered 20/1, 2011 at 10:25 Comment(0)
L
0

A solutiion could be this:

ProgressDialog progressDialog = null;
    // ...
    progressDialog = ProgressDialog.show(this, "Please wait...", true);
    new Thread() {
        public void run() {
            try{
                  // Grab your data                                                
            } catch (Exception e) { }

            // When grabbing data is finish: Dismiss your Dialog 
            progressDialog.dismiss();
        }
   }.start();
Lapboard answered 20/1, 2011 at 10:22 Comment(1)
That will cause a crash if the activity is destroyed before you get to the progressDialog.dismiss() lineAdrianople

© 2022 - 2024 — McMap. All rights reserved.