Reputation: 55227
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.
Upvotes: 0
Views: 789
Reputation: 200090
In that case you must override and hook the DialogInterface.OnClickListener
and, from its implementation, set the value of orderBy
. In fact, this is a better aproach:
case SORT_MENU:
showDialog(ORDER_DIALOG);
break;
Then, on onCreateDialog
:
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();
showDialog(COLUMNS_DIALOG); // <-- NEW!!!
}
});
dialog = orderBuilder.create();
break;
Again, on onCreateDialog
:
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();
configureString(); // <-- NEW!
}
});
dialog = columnBuilder.create();
break;
Somewhere else:
private void configureString(){
String orderBy = bundle.getString("column") + bundle.getString("order");
}
Upvotes: 2