It seems to be there is no easy way to get an Alert dialog to return a simple value.
This code does not work (the answer variable cannot be set
I was also struggling to use a blocking confirm dialog and I finally did it using a BlockingQueue :
public static class BlockingConfirmDialog{
private Activity context;
BlockingQueue blockingQueue;
public BlockingConfirmDialog(Activity activity) {
super();
this.context = activity;
blockingQueue = new ArrayBlockingQueue(1);
}
public boolean confirm(final String title, final String message){
context.runOnUiThread(new Runnable() {
@Override
public void run() {
new AlertDialog.Builder(context)
.setTitle(title)
.setMessage(message)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
blockingQueue.add(true);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
blockingQueue.add(false);
}
})
.show();
}
});
try {
return blockingQueue.take();
} catch (InterruptedException e) {
e.printStackTrace();
return false;
}
}
}