calling method from alertdialog onclick event within ui thread
<p>I am having an issue calling a method within an OnClick event of an AlertDialog that's within a RunOnUiThread.</p>

<p>The logic below is that ProgressDialog displays while the <em>applyUpdate()</em> method is executing. The method returns 0 if the the download has failed or is still in progress. The user is given the choice to restart or continue the download. </p>

<p>The issue is that when <em>Restart</em> is selected the AlertDialog remains on the screen (with the button clicked).</p>

<p>Is the doDownload() starting before the dialog dismisses, is it a threading issue, or something else?</p>

<pre><code>public void doApply(View view){
    progressDialog = ProgressDialog.show(UpdateForm.this, "", "Applying update...");

    runOnUiThread(new Runnable() {

        @Override
        public void run() {
            try{
                applyUpdate = nnEngine.applyUpdate();
            } catch (Exception e) {
                // Handle exception
            }

            // Check update result.
            progressDialog.dismiss();
            switch (applyUpdate) {
// Other cases removed.....
                case 0:
                    // Download in progress or failed.
                    AlertDialog.Builder inprogDlg = new AlertDialog.Builder(UpdateForm.this);
                    inprogDlg.setTitle("Download in progress or failed.");

                    inprogDlg.setPositiveButton("Continue download", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();

                        // Wrong button may be displayed.
                        notifyUser(null, checkDownloadButton);
                    }
                });

                inprogDlg.setNegativeButton("Restart download", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        doDownload(getWindow().getDecorView().findViewById(android.R.id.content));
                    }
                });

                inprogDlg.create().show();
                break;
            }
        }
    });
}
</code></pre>
