views:

45

answers:

3

Hello,

I have an android app where I am doing the following:

private void onCreate() {
    final ProgressDialog dialog = ProgressDialog.show(this, "Please wait..", "Doing stuff..", true);

    new Thread() {
        public void run() {
            //do some serious stuff...
            dialog.dismiss();           
        }
    }.start(); 

    stepTwo();
}

And I would like to ensure that my thread is complete before stepTwo(); is called. How can I do this?

Thanks!

A: 

The Thread instance has a join method, so:

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();

}

You may want to try this though:

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...
            SwingUtilities,invokeLater(new Runnable() {
                public void run() {
                    dialog.dismiss();           
                }
            });
            stepTwo();
        }
    };
    t.start(); 
}

Because onCreate is in the UI thread, having the join in there will freeze the UI till after onCreate completes, saving any dialog till then. stepTwo will have to use SwingUtilities.invokeLater to do any UI changes itself.

sblundy
The `dialog.dismis()` should take place in the UI-thread if I'm not mistaken.
aioobe
@aioobe - I expect you're right. That's what SwingUtilities.invokeLater is for. Unless android is different
sblundy
Thanks sblundy - This is working for the most part, however my dialog doesn't appear as I believe it should. Basically what happens is that it appears that it does the tasks in the thread correctly, and then calls stepTwo(); and THEN the dialog box appears for a split second and disappears. Any ideas how to make it show up before it starts the thread?
Tyler
+2  A: 

If you want to run things in the background I'd recommend using the AsyncTask class as doing so will ensure you interact with the UI thread properly.

Also, if you want code to run after the background task is complete you can just call the method then. There's no reason wait inside onCreate().

Your code will look something like this:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    new MyAsyncTask().execute();
}

private class MyAsyncTask extends AsyncTask<Void, Void, Void> {
    private ProgressDialog dialog;

    @Override
    protected void onPreExecute() {
        dialog = ProgressDialog.show(MyActivity.this, "Please wait..", "Doing stuff..", true);  
    }

    @Override
    protected Void doInBackground(Void... params) {
        //do some serious stuff...
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        dialog.dismiss();
        stepTwo();
    }

}
Dave Webb
A: 

Another, option is to simply move step2() into the thread so it executes after the thread tasks complete:

private void onCreate() {
    final ProgressDialog dialog = ProgressDialog.show(this, "Please wait..", "Doing stuff..", true);

    new Thread() {
        public void run() {
            //do some serious stuff...
            dialog.dismiss();           
            stepTwo();
        }
    }.start(); 
}
Brad Hein