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
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabstract booleancancel(boolean mayInterruptIfRunning) Attempts to cancel execution of this task.protected abstract RdoInBackground(T params) This method will be called for doing the background work.abstract voidfinish(DynamicResult<R> result) This method will be called to handle the result returned by the task.abstract booleanReturnstrueif this task was cancelled before it completed normally.protected voidApplications should preferably overrideonCancelled(DynamicResult).protected voidonCancelled(DynamicResult<R> result) Runs on the UI thread aftercancel(boolean)is invoked anddoInBackground(Object)has finished.protected voidonPostExecute(DynamicResult<R> result) This method will be called after completing the work.protected voidThis method will be called before doing the background work.protected voidonProgressUpdate(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()Returnstrueif 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:
trueif 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 whencancelis called, this task should never run. If the task has already started, then themayInterruptIfRunningparameter 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 ifcancelreturns 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
mayInterruptIfRunningis true.- Parameters:
mayInterruptIfRunning-trueif the thread executing this task should be interrupted; otherwise, in-progress tasks are allowed to complete.- Returns:
falseif the task could not be cancelled, typically because it has already completed normally;trueotherwise- See Also:
-