Class DynamicRunnable<T,P,R>
java.lang.Object
com.pranavpandey.android.dynamic.util.concurrent.DynamicRunnable<T,P,R>
- Type Parameters:
T
- The type of the params.P
- The type of the progress.R
- The type of the result.
- All Implemented Interfaces:
Runnable
- Direct Known Subclasses:
DynamicTask
Base class to receive the callback from an asynchronous work.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionabstract boolean
cancel
(boolean mayInterruptIfRunning) Attempts to cancel execution of this task.protected abstract R
doInBackground
(T params) This method will be called for doing the background work.abstract void
finish
(DynamicResult<R> result) This method will be called to handle the result returned by the task.abstract boolean
Returnstrue
if this task was cancelled before it completed normally.protected void
Applications should preferably overrideonCancelled(DynamicResult)
.protected void
onCancelled
(DynamicResult<R> result) Runs on the UI thread aftercancel(boolean)
is invoked anddoInBackground(Object)
has finished.protected void
onPostExecute
(DynamicResult<R> result) This method will be called after completing the work.protected void
This method will be called before doing the background work.protected void
onProgressUpdate
(DynamicResult<P> progress) This method will be called on publishing the progress.abstract DynamicResult<P>
publishProgress
(DynamicResult<P> progress) This method can be invoked fromdoInBackground(T)
to publish updates on the UI thread while the background computation is still running.
-
Constructor Details
-
DynamicRunnable
public DynamicRunnable()
-
-
Method Details
-
onPreExecute
This method will be called before doing the background work. -
doInBackground
This method will be called for doing the background work.- Parameters:
params
- The optional parameters required for the work.- Returns:
- The optional result object.
-
onCancelled
Applications should preferably overrideonCancelled(DynamicResult)
. This method is invoked by the default implementation ofonCancelled(DynamicResult)
.The default version does nothing.
Runs on the UI thread after
cancel(boolean)
is invoked anddoInBackground(Object)
has finished. -
isCancelled
public abstract boolean isCancelled()Returnstrue
if this task was cancelled before it completed normally. If you are callingcancel(boolean)
on the task, the value returned by this method should be checked periodically fromdoInBackground(Object)
to end the task as soon as possible.- Returns:
true
if task was cancelled before it completed- See Also:
-
cancel
public abstract boolean cancel(boolean mayInterruptIfRunning) Attempts to cancel execution of this task. This attempt will fail if the task has already completed, already been cancelled, or could not be cancelled for some other reason. If successful, and this task has not started whencancel
is called, this task should never run. If the task has already started, then themayInterruptIfRunning
parameter determines whether the thread executing this task should be interrupted in an attempt to stop the task.Calling this method will result in
onCancelled(DynamicResult)
being invoked on the UI thread afterdoInBackground(Object)
returns. Calling this method guarantees that onPostExecute(Object) is never subsequently invoked, even ifcancel
returns false, butonPostExecute(com.pranavpandey.android.dynamic.util.concurrent.DynamicResult<R>)
has not yet run. To finish the task as early as possible, checkisCancelled()
periodically fromdoInBackground(Object)
.This only requests cancellation. It never waits for a running background task to terminate, even if
mayInterruptIfRunning
is true.- Parameters:
mayInterruptIfRunning
-true
if the thread executing this task should be interrupted; otherwise, in-progress tasks are allowed to complete.- Returns:
false
if the task could not be cancelled, typically because it has already completed normally;true
otherwise- See Also:
-