Value
- task returned value type.public abstract class AsyncTask<Value> extends Object
execute()
abstract method.
There are basically two usage patterns for handling the returned value: first is to override onPostExecute(Object)
like in sample code below. Please note that value handling hook is executed in the same thread as task logic.
AsyncTask<Object> task = new AsyncTask<Object>() { protected Object execute() throws Throwable { // asynchronous task logic } protected void onPostExecute(Object value) { // take some actions on task returned value // executed in the same thread as execute() } }; task.start();
Second is supplying a Callback
implementation to asynchronous task constructor. After execute()
completes,
Callback.handle(Object)
is transparently invoked in the same thread on which task was executed.
AsyncTask<Object> task = new AsyncTask<Object>(callback) { protected Object execute() throws Throwable { // asynchronous task logic; } }; task.start();
Because of his nature, asynchronous logic exceptions are not directly accessible to caller thread. Once asynchronous thread
launched the caller thread has no chance to catch execution exceptions. To overcome this limitation this class supply
onThrowable(Throwable)
hook. If exception occur neither onPostExecute(Object)
nor
Callback.handle(Object)
are invoked.
AsyncTask<Object> task = new AsyncTask<Object>(callback) { protected Object execute() throws Throwable { // asynchronous task logic; } protected void onThrowable(Throwable throwable) { // handle asynchronous execution exception } }; task.start();
Modifier and Type | Field and Description |
---|---|
protected Callback<Value> |
callback
Asynchronous task listener.
|
Modifier | Constructor and Description |
---|---|
protected |
AsyncTask()
Constructor for tasks that may use
onPostExecute(Object) for returned value handling. |
protected |
AsyncTask(Callback<Value> callback)
Constructor for task using callback for returning value handling.
|
Modifier and Type | Method and Description |
---|---|
protected abstract Value |
execute()
Execute task logic and return a resulting value.
|
protected void |
onPostExecute(Value value)
Task returned value handler.
|
protected void |
onPreExecute()
Hook executed before task logic execution.
|
protected void |
onThrowable(Throwable throwable)
Hook executed when task logic fails.
|
void |
start()
Start asynchronous task execution.
|
protected AsyncTask()
onPostExecute(Object)
for returned value handling. See first usage pattern
from class description.protected abstract Value execute() throws Throwable
Throwable
- any exception task logic may throw.public void start()
protected void onPreExecute()
protected void onPostExecute(Value value)
callback
, of course if one supplied to constructor.value
- value returned by task logic.protected void onThrowable(Throwable throwable)
throwable
- exception generated by failed task logic.Copyright © 2018. All rights reserved.