public abstract class BackgroundTask<P>
extends java.lang.Object
AsyncTask
and store caller (parent object) into
WeakReference
. All derived inner classes should be marked as static in most cases
to not leak any Activities
or Fragments
Constructor and Description |
---|
BackgroundTask(P parent) |
Modifier and Type | Method and Description |
---|---|
void |
cancel(boolean mayInterruptIfRunning) |
protected abstract void |
doTask() |
void |
exec() |
protected void |
onTaskEnded(P parent) |
protected void |
onTaskFail(P parent,
java.lang.Exception e) |
protected void |
onTaskProgress(P parent) |
protected void |
onTaskStarted(P parent) |
protected void |
onTaskSuccess(P parent) |
protected void |
publishTaskProgress()
Calls
AsyncTask.publishProgress() method. |
protected void |
setSkipCallbacksIfParentIsNull(boolean skip) |
public BackgroundTask(P parent)
parent
- Parent object (i.e. Activity
or
Fragment
) to be passed to task's lyfecycle
methods (onTaskStarted
,
onTaskSuccess
,
onTaskFail
or onTaskEnded
)protected void onTaskStarted(P parent)
protected abstract void doTask() throws java.lang.Exception
java.lang.Exception
protected void setSkipCallbacksIfParentIsNull(boolean skip)
skip
- Parent object is saved into WeakReference
so it can be erased and we won't
be able to pass it to task's lyfecycle methods. Default behavior is to skip
calling these methods if parent object is null
to prevent NPE,
but you can change this if you must be sure that all callback methods are
always called.protected void publishTaskProgress()
AsyncTask.publishProgress()
method.
Corresponding callback method is onTaskProgress
.doTask
method.protected void onTaskProgress(P parent)
protected void onTaskSuccess(P parent)
protected void onTaskFail(P parent, java.lang.Exception e)
protected void onTaskEnded(P parent)
public void exec()
public void cancel(boolean mayInterruptIfRunning)