Here is the relevant code:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case SORT_MENU:
showDialog(ORDER_DIALOG);
showDialog(COLUMNS_DIALOG);
String orderBy = bundle.getString("column") + bundle.getString("order");
break;
}
return false;
}
@Override
protected Dialog onCreateDialog(int id) {
Dialog dialog;
final String[] columns = { "title", "completed" };
final String[] order = { "Ascending", "Descending" };
switch (id) {
case COLUMNS_DIALOG:
AlertDialog.Builder columnBuilder = new AlertDialog.Builder(this);
columnBuilder.setTitle("Columns");
columnBuilder.setItems(columns, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
bundle.putString("column", columns[which]);
dialog.dismiss();
}
});
dialog = columnBuilder.create();
break;
case ORDER_DIALOG:
AlertDialog.Builder orderBuilder = new AlertDialog.Builder(this);
orderBuilder.setTitle("Order");
orderBuilder.setItems(order, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String orderS;
if (order[which].equalsIgnoreCase("Ascending"))
orderS = "ASC";
else
orderS = "DESC";
bundle.putString("order", orderS);
dialog.dismiss();
}
});
dialog = orderBuilder.create();
break;
default:
dialog = null;
break;
}
return dialog;
}
I know that a Dialog is asynchronous from the main UI thread. So calls like these:
showDialog(ORDER_DIALOG);
showDialog(COLUMNS_DIALOG);
String orderBy = bundle.getString("column") + bundle.getString("order");
Only result in orderBy being null. Is there any way to have orderBy wait until both dialogs are confirmed to be finished? Even if dialog.isShowing() is false, this may be because the dialog has already finished or hasnt’t even started.
In that case you must override and hook the
DialogInterface.OnClickListenerand, from its implementation, set the value oforderBy. In fact, this is a better aproach:Then, on
onCreateDialog:Again, on
onCreateDialog:Somewhere else: