views:

106

answers:

2

Okay.. I am doing something similar to the below:

private void onCreate() {
    final ProgressDialog dialog = ProgressDialog.show(this, "Please wait..", "Doing stuff..", true);
Thread t = new Thread() {
    public void run() {
        //do some serious stuff...
        dialog.dismiss();           
    }
};
t.start(); 
t.join();
stepTwo();

}

However, what I am finding is that my progress dialog never even shows up. My App stalls for a moment so I know it is chugging along inside of thread t, but why doesnt my dialog appear?

IF I remove the line:

t.join();

Then what I find happens is that the progress dialog does show up, but my app starts stepTwo(); before what happens in the thread is complete..

Any ideas?

+1  A: 

Try to use an handler

public class MyActivity {

private Handler handler;

private void onCreate() {

   handler = new Handler() {

      @Override
      public void handleMessage(Message msg) {
         pd.dismiss();
         stepTwo();
      }
   };

   final ProgressDialog dialog = ProgressDialog.show(this, "Please wait..", "Doing stuff..", true);
   Thread t = new MyThread() {
   t.start():

}

private class MyThread extends Thread() {
   public void run() {
      //do some serious stuff...
      handler.sendEmptyMessage(0);       
   }
}   

}
Roflcoptr
Thanks for the suggestion Sebi - I have never worked with Handlers before.. I attempted the implementation as you have it, but received some errors (specifically aroundprivate class MyThread extends Thread()) So I was unable to get this to work.. Is there no easier solution in my initial code to ensure PD appears?
Tyler
Thread t = new MyThread() { t.start(): };this is wrong. the correct is Thread t = new MyThread();t.start();
Omer
A: 

Your join() line blocks the UI thread that runs the ProgressDialog. You are therefore blocking layouts, drawings, etc.

Romain Guy
I see. So a handler is the only way around this?
Tyler