I don't know how to elegantly solve the following task:
- I have several blocks of code (operation) to execute.
- Each block can return
true
offalse
to indicate that further execution is possible. - Inside of each block I have to use asyncronous methods calls (Because Android is completeley asynchronous).
Example of processing operations (not working now as expected):
List<Operation> operations = command.getOperations();
for (Operation operation : operations) {
Log.d(TAG, "Processing operation: " + operation);
OperationResult result = operation.execute(activity);
Log.d(TAG, "Operation result is: " + result);
if (!result.canContinue()) {
break;
}
}
The problem is that inside of the operation I need, for example, display AlertDialog and wait for the input. But after I call dialog.show()
my method execute
finishes and it returns incorrect result.
Example of the button listener, registerd with AlertDialog is below:
final OperationResult result = new OperationResult();
final class ButtonListener implements DialogInterface.OnClickListener {
public void onClick(DialogInterface dialog, int id) {
switch (id) {
case DialogInterface.BUTTON_POSITIVE: {
result.setCanContinue(true);
}
case DialogInterface.BUTTON_NEGATIVE: {
dialog.cancel();
result.setCanContinue(false);
}
}
}
How should I modify processing of operations to support asynchronous model of Android?