|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object android.os.AsyncTask<ParameterT,ProgressT,ReturnT> com.github.ignition.core.tasks.IgnitedAsyncTask<ContextT,ParameterT,ProgressT,ReturnT>
ContextT
- ParameterT
- ProgressT
- ReturnT
- public abstract class IgnitedAsyncTask<ContextT extends android.content.Context,ParameterT,ProgressT,ReturnT>
An extension to AsyncTask that removes some of the boilerplate that's typically involved. Some of the things handled for you by this class are:
Context
reference for you, which is passed to all status callbacksonTaskFailed(Context, Exception)
IgnitedAsyncTask.IgnitedAsyncTaskCallable
Context
to register itself as a callback handler via
IgnitedAsyncTaskContextHandler
IgnitedAsyncTaskHandler
Since this class keeps a reference to a Context (either directly or indirectly through a callback
handler), you MUST ensure that this reference is cleared when the Context gets destroyed. You can
handle Context and handler connection and disconnection using the connect(ContextT)
and
disconnect()
methods. For Activities, a good place to call them is onCreate and onDestroy
respectively.
Please note that the callbacks added by this class will ONLY be called if the context reference is valid. You can still receive AsyncTask's callbacks by overriding the callbacks defined in that class.
Nested Class Summary | |
---|---|
static interface |
IgnitedAsyncTask.IgnitedAsyncTaskCallable<ContextT extends android.content.Context,ParameterT,ProgressT,ReturnT>
|
Nested classes/interfaces inherited from class android.os.AsyncTask |
---|
android.os.AsyncTask.Status |
Field Summary | |
---|---|
protected boolean |
cancelOnActivityBack
|
Constructor Summary | |
---|---|
IgnitedAsyncTask()
|
Method Summary | |
---|---|
void |
connect(ContextT context)
Connects a context object to this task. |
void |
connect(IgnitedAsyncTaskHandler<ContextT,ProgressT,ReturnT> handler)
Connects an IgnitedAsyncTaskHandler to this task to receive callbacks. |
void |
disconnect()
Disconnects all context references, also those that are both a Context and handler at the same time. |
protected ReturnT |
doInBackground(ParameterT... params)
No, you want to override run(Object...) instead. |
boolean |
failed()
|
ContextT |
getContext()
Returns the task's current context reference. |
IgnitedAsyncTaskContextHandler<ProgressT,ReturnT> |
getContextHandler()
If you have connected a Context which implements the IgnitedAsyncTaskContextHandler
interface, this instance will be returned. |
IgnitedAsyncTaskHandler<ContextT,ProgressT,ReturnT> |
getDelegateHandler()
If you have connected a Context or POJO which implements the IgnitedAsyncTaskHandler
interface, this instance will be returned. |
boolean |
isCancelOnActivityBack()
|
boolean |
isFinished()
|
boolean |
isPending()
|
boolean |
isRunning()
|
protected void |
onPostExecute(ReturnT result)
Don't override this method; use onTaskCompleted(ContextT, ReturnT) , onTaskSuccess(ContextT, ReturnT) , and
onTaskFailed(ContextT, java.lang.Exception) instead. |
protected void |
onPreExecute()
If you rely on a valid context reference, override onTaskStarted(Context) instead. |
protected void |
onProgressUpdate(ProgressT... values)
If you rely on a valid context reference, override #onProgress instead. |
boolean |
onTaskCompleted(ContextT context,
ReturnT result)
Implement this method to handle a completed task execution, regardless of outcome. |
boolean |
onTaskCompleted(ReturnT result)
Implement this method to handle a completed task execution, regardless of outcome. |
boolean |
onTaskFailed(ContextT context,
Exception error)
Override this method to handle an error that occurred during task execution in a graceful manner. |
boolean |
onTaskFailed(Exception error)
Override this method to handle an error that occurred during task execution in a graceful manner. |
boolean |
onTaskProgress(ContextT context,
ProgressT... progress)
Override this method to update progress elements on the UI thread. |
boolean |
onTaskProgress(ProgressT... progress)
Override this method to update progress elements on the UI thread. |
boolean |
onTaskStarted()
Override this method to prepare task execution. |
boolean |
onTaskStarted(ContextT context)
Override this method to prepare task execution. |
boolean |
onTaskSuccess(ContextT context,
ReturnT result)
Implement this method to handle a successful task execution. |
boolean |
onTaskSuccess(ReturnT result)
Implement this method to handle a successful task execution. |
ReturnT |
run(ParameterT... params)
Implement this method to define your task execution. |
void |
setCallable(IgnitedAsyncTask.IgnitedAsyncTaskCallable<ContextT,ParameterT,ProgressT,ReturnT> callable)
Use an IgnitedAsyncTask.IgnitedAsyncTaskCallable instead of overriding run(ParameterT...) . |
void |
setCancelOnActivityBack(boolean cancelOnActivityBack)
|
void |
setContext(ContextT context)
DON'T use this to handle the task's context reference. |
Methods inherited from class android.os.AsyncTask |
---|
cancel, execute, get, get, getStatus, isCancelled, onCancelled, publishProgress |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
protected boolean cancelOnActivityBack
Constructor Detail |
---|
public IgnitedAsyncTask()
Method Detail |
---|
public void setCancelOnActivityBack(boolean cancelOnActivityBack)
public boolean isCancelOnActivityBack()
public void connect(ContextT context)
Activity.onCreate(android.os.Bundle)
to re-connect tasks that you retained via
Activity.onRetainNonConfigurationInstance()
. Make sure to always
disconnect()
a context in Activity.onDestroy()
.
As long as this context is not null, it will be passed to any handler callbacks associated
with this task, as well as to the task's own callbacks. If the context you pass here
implements IgnitedAsyncTaskContextHandler
, it will be registered to receive callbacks
itself. If instead it implements the more generic IgnitedAsyncTaskHandler
, and if
no other handler of this type has been connected before (!), then the context will also
receive these callbacks. If however an ordinary POJO had already been connected to this task
as a IgnitedAsyncTaskHandler
, then the context will not receive callbacks via this
interface (this is because a context must be disconnected from the task in onDestroy, while
ordinary handlers that are not Contexts will be retained by the task instance).
public void connect(IgnitedAsyncTaskHandler<ContextT,ProgressT,ReturnT> handler)
IgnitedAsyncTaskHandler
to this task to receive callbacks. The context
instance wrapped by this handler will be passed to connect(Context)
.
handler
- the handler object to receive task callbackspublic void disconnect()
Context#onDestroy
. Note that this will NOT
disconnect handlers which are not Contexts but plain POJOs, so make sure you do not leak an
implicit reference to the same context from any handlers you have connected.
public ContextT getContext()
connect(Context)
and disconnect()
.
getContext
in interface IgnitedAsyncTaskHandler<ContextT extends android.content.Context,ProgressT,ReturnT>
public void setContext(ContextT context)
connect(Context)
and
disconnect()
instead.
setContext
in interface IgnitedAsyncTaskHandler<ContextT extends android.content.Context,ProgressT,ReturnT>
public IgnitedAsyncTaskContextHandler<ProgressT,ReturnT> getContextHandler()
IgnitedAsyncTaskContextHandler
interface, this instance will be returned. Null otherwise.
public IgnitedAsyncTaskHandler<ContextT,ProgressT,ReturnT> getDelegateHandler()
IgnitedAsyncTaskHandler
interface, this instance will be returned. Null otherwise.
protected void onPreExecute()
onTaskStarted(Context)
instead.
onPreExecute
in class android.os.AsyncTask<ParameterT,ProgressT,ReturnT>
public boolean onTaskStarted(ContextT context)
onTaskStarted
in interface IgnitedAsyncTaskHandler<ContextT extends android.content.Context,ProgressT,ReturnT>
context
- The most recent instance of the Context that executed this IgnitedAsyncTaskAsyncTask#onPreExecute}
public boolean onTaskStarted()
onTaskStarted
in interface IgnitedAsyncTaskContextHandler<ProgressT,ReturnT>
AsyncTask#onPreExecute}
protected void onProgressUpdate(ProgressT... values)
#onProgress
instead.
onProgressUpdate
in class android.os.AsyncTask<ParameterT,ProgressT,ReturnT>
public boolean onTaskProgress(ContextT context, ProgressT... progress)
onTaskProgress
in interface IgnitedAsyncTaskHandler<ContextT extends android.content.Context,ProgressT,ReturnT>
context
- The most recent instance of the Context that executed this IgnitedAsyncTaskprogress
- the progress valuesAsyncTask#publishProgress}
,
AsyncTask#onProgressUpdate}
public boolean onTaskProgress(ProgressT... progress)
onTaskProgress
in interface IgnitedAsyncTaskContextHandler<ProgressT,ReturnT>
progress
- the progress valuesAsyncTask#publishProgress}
,
AsyncTask#onProgressUpdate}
protected final ReturnT doInBackground(ParameterT... params)
run(Object...)
instead.
doInBackground
in class android.os.AsyncTask<ParameterT,ProgressT,ReturnT>
public ReturnT run(ParameterT... params) throws Exception
IgnitedAsyncTask.IgnitedAsyncTaskCallable
via
setCallable(IgnitedAsyncTaskCallable)
Typically you do not call this method directly; instead it's called by #execute()
and
run on a worker thread. If however you want to execute the task synchronously, you can invoke
this method directly.
params
- the parameters for your task
Exception
AsyncTask#doInBackground}
protected void onPostExecute(ReturnT result)
onTaskCompleted(ContextT, ReturnT)
, onTaskSuccess(ContextT, ReturnT)
, and
onTaskFailed(ContextT, java.lang.Exception)
instead.
onPostExecute
in class android.os.AsyncTask<ParameterT,ProgressT,ReturnT>
public boolean onTaskCompleted(ContextT context, ReturnT result)
onTaskCompleted
in interface IgnitedAsyncTaskHandler<ContextT extends android.content.Context,ProgressT,ReturnT>
context
- The most recent instance of the Context that executed this IgnitedAsyncTask the
result of the task executionAsyncTask#onPostExecute}
public boolean onTaskCompleted(ReturnT result)
onTaskCompleted
in interface IgnitedAsyncTaskContextHandler<ProgressT,ReturnT>
result
- the result of the task executionAsyncTask#onPostExecute}
public boolean onTaskSuccess(ContextT context, ReturnT result)
onTaskSuccess
in interface IgnitedAsyncTaskHandler<ContextT extends android.content.Context,ProgressT,ReturnT>
context
- The most recent instance of the Context that executed this IgnitedAsyncTaskresult
- the result of the task executionpublic boolean onTaskSuccess(ReturnT result)
onTaskSuccess
in interface IgnitedAsyncTaskContextHandler<ProgressT,ReturnT>
result
- the result of the task executionpublic boolean onTaskFailed(ContextT context, Exception error)
onTaskFailed
in interface IgnitedAsyncTaskHandler<ContextT extends android.content.Context,ProgressT,ReturnT>
context
- The most recent instance of the Context that executed this IgnitedAsyncTaskerror
- The exception that was thrown during task executionpublic boolean onTaskFailed(Exception error)
onTaskFailed
in interface IgnitedAsyncTaskContextHandler<ProgressT,ReturnT>
error
- The exception that was thrown during task executionpublic boolean failed()
run(ParameterT...)
public boolean isPending()
AsyncTask.Status.PENDING
public boolean isRunning()
AsyncTask.Status.RUNNING
public boolean isFinished()
AsyncTask.Status.FINISHED
public void setCallable(IgnitedAsyncTask.IgnitedAsyncTaskCallable<ContextT,ParameterT,ProgressT,ReturnT> callable)
IgnitedAsyncTask.IgnitedAsyncTaskCallable
instead of overriding run(ParameterT...)
. This can be
useful when creating task skeletons that need to implement the worker method differently.
callable
-
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |