0

I've found that a lot of people are having a similar problem but I am simply trying to show a dialog while I am grabbing some data off a URL and then dismiss properly after the data is retrieved. Here is what I'm trying to do (This is in my onClick() method for a refresh button):

dialog.show();  
// do some work  
dialog.dismiss();

Doing it this way you never really see the dialog at all. I've tried doing it using an extra thread such as:

Thread t = new Thread() {
    public void run() {
        dialog.show();
    }
};

But this way I get an error and a force close down...
What is the best method to do this?

Korvin Szanto
  • 4,531
  • 4
  • 19
  • 49
Fobos13
  • 113
  • 1
  • 1
  • 11
  • check this http://codinglookseasy.blogspot.in/2012/10/save-image-in-sdcard.html here in the entire code there is a progress dialog in the onclick() – G_S Oct 17 '12 at 05:38
  • Use AsyncTask as it will be the best for this kind of task. – Scorpion Oct 17 '12 at 05:39

3 Answers3

0

For this kind of task mostly I recommend you to use the AsyncTask. Here is one good example of it which will help you.

And for this question as You are making UI related task and for that please use the UI thread.

runOnUiThread(new Runnable() {

    @Override
    public void run() {
    // TODO Auto-generated method stub
    //Show or Hide your ProgressDialog
    }
});
Community
  • 1
  • 1
Scorpion
  • 6,831
  • 16
  • 75
  • 123
0

For the task you are trying to implement:

dialog.show();  
// do some work  
dialog.dismiss();

Now, to implement above, there is concept of AsyncTask, the best way to implement Threading task, as its also known as Painless Threading in Android.

AsyncTask has 4 main methods:

  1. onPreExecute() - Here you can show ProgressDialog or ProgressBar.
  2. doInBackground() - Here you can do/implement background task
  3. onProgressUpdate() - Here you can update UI based on the intermediate result you receive from webservice call or from background task
  4. onPostExecute() - Here you can dismiss dialog or make progress bar invisible. Also you can do the task which want to do after receiving result from background task or webservice call.
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
  • 1
    This is helpful thank you. I also just found someone who did this with a handler (see link) but it looks like what you have is more robust. [link] http://www.helloandroid.com/tutorials/using-threads-and-progressdialog – Fobos13 Oct 17 '12 at 06:25
  • @user1752041 yes thats also a solution, but why should we android developer need to bother about threading management if we already have something fantastic (i.e. AsyncTask) – Paresh Mayani Oct 17 '12 at 06:48
0

Try to use runOnUiThread of the Activity instead of using Thread. Check the following code snippet. runOnUiThread(new Runnable() {

            public void run() {

                    dialog = new ProgressDialog(ctContext);

                    dialog.show();
                }
            }

        });

I think this will help you.

Raj
  • 1,843
  • 2
  • 21
  • 47