Seems there is several variations on this theme. I display an AlertBox to the user do want to save this item out? If they respond OK then I want the Alertbox to go away to be replaced by ProgressDialog box then when item is done being saved out it dismisses.
The current code below shows the Ok/Cancel AlertBox and dismiss properly and shows toast correctly. But if the user selects OK the ProgressDialog shows after all is completed and never goes away. The OK/Cancel button stays depressed until item is saved. If the user presses OK I want the AlertBox to go away and then display the ProgressDialog and dismiss it when done saving.
{
Vibrate(ClickVibrate);
final ProgressDialog Dialog = ProgressDialog.show(v.getRootView().getContext(), "Loading", "Please wait...", true);
if(AlertDialogProcessing==0)
{
ProgressDialog progress;
final String title="Save Item";
final String message="Press OK to save or CANCEL.";
final String ok="OK";
final String cancel="CANCEL";
final AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
alertbox.setCancelable(true);
alertbox.setIcon(android.R.drawable.ic_dialog_alert);
alertbox.setTitle(title);
alertbox.setMessage(message);
alertbox.setNegativeButton(cancel, null);
final AlertDialog dlg = alertbox.create();
alertbox.setPositiveButton(ok,new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface arg0, int arg1)
{
dlg.dismiss();
Dialog.show();
Vibrate(ClickVibrate);
Drawable drawable= getItem(imageSelect);
AlertDialogProcessing=1;
//task that takes 3 seconds
AlertDialogProcessing=0;
Toast.makeText(getApplicationContext(), "Item Saved.", Toast.LENGTH_LONG).show();
}
});
alertbox.setNegativeButton(cancel,new DialogInterface.OnClickListener(){ public void onClick(DialogInterface arg0, int arg1){AlertDialogProcessing=0; Vibrate(ClickVibrate); } });
alertbox.show();
}
Dialog.dismiss();
}
You’re calling both
ProgressDialog.show()andProgressDialog.dismiss()inside the positive button’sonClick(), no wonder theProgressDialogdoesn’t show. If the saving process takes some time such that you need aProgressDialog, I’d strongly recommend using theAsyncTaskclass. It runs a task on a worker thread and gives you a possibility to update the UI with the current task progress. Hope this helps.