I would like to add that continueWith and continueWithTask really caused me problems, obviously because I really did not understand the API, but also the naming confused me. Perhaps an example of my refusal may prevent others from doing the same.
TL; DR
When to use which method:
Use continueWith if you want to use the result of the previous task and return the new result to the Continuation then method. And you need to pass it on to another sequel or use it later in the listeners. Return value continueWith is a task that simply WRAPS returns the return value of the then method.
Use continueWithTask if you want to use the result of the previous task, and use it a little in the new task that you create in your Continuation then method. The return value of continueWithTask is the task that you create inside then and has its own common result.
DO NOT return the task in its continuation and use it with continueWith. It can compile and run for years without warning, but also without doing this work.
IF you directly add a listener after continueWith this listener will give you the task TO REVERSE the result of your return then cost. If this return value is the task itself, do not expect it to be executed (!!!).
Long story!
I had a chain of calls like:
private Task<SomeResult> getTask() { PreloadingTask.create().continueWith(additionalTask()).addOnCompleteListener(task -> { if (task.isSuccessful()) { source.setResult(someResult); } else { source.setException(new Exception()); } }); return source.getTask(); }
So, as you can see, additionalTask() should return some type of Continuation<IN, OUT> , which implements the method, and then
@Override public OUT then(Task<IN> task){ ... }};
In my case, I did not need to check OUT , because I just wanted to do additional calculations AFTER the PreloadingTask and redirects were completed, this would lead to the continuation of additionalTask() .
I wanted to execute a task from additionalTask() , and then onCompleteListener should be called.
private Continuation<PreviousResult, Task<Void>> additionalTask() { return task -> { PreviousResult r = task.getResult(); simpleResultModification(r); return new AdditionalTask(r); ); }; }
What happened? onCompleteListener was called directly because my then method was executed and returned its result, which was an instance of AdditionalTask . Then this AdditionalTask wrapped in another task and passed the result to onCompleteListener.
And my ExtraText was not executed.
What you should use continueWithTask if you return the task during this time.