Class 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:
    java.lang.Runnable
    Direct Known Subclasses:
    DynamicTask

    public abstract class DynamicRunnable<T,​P,​R>
    extends java.lang.Object
    implements java.lang.Runnable
    Base class to receive the callback from an asynchronous work.
    • Constructor Detail

      • DynamicRunnable

        public DynamicRunnable()
    • Method Detail

      • onPreExecute

        @MainThread
        protected void onPreExecute()
        This method will be called before doing the background work.
      • doInBackground

        @WorkerThread
        @Nullable
        protected abstract R doInBackground​(@Nullable
                                            T params)
        This method will be called for doing the background work.
        Parameters:
        params - The optional parameters required for the work.
        Returns:
        The optional result object.
      • onProgressUpdate

        @MainThread
        protected void onProgressUpdate​(@Nullable
                                        DynamicResult<P> progress)
        This method will be called on publishing the progress.
        Parameters:
        progress - The progress returned by the work.
      • onPostExecute

        @MainThread
        protected void onPostExecute​(@Nullable
                                     DynamicResult<R> result)
        This method will be called after completing the work.
        Parameters:
        result - The result returned by the work.
      • finish

        @MainThread
        public abstract void finish​(@Nullable
                                    DynamicResult<R> result)
        This method will be called to handle the result returned by the task.
        Parameters:
        result - The result returned by the work.
      • isCancelled

        public abstract boolean isCancelled()
        Returns true if this task was cancelled before it completed normally. If you are calling cancel(boolean) on the task, the value returned by this method should be checked periodically from doInBackground(Object) to end the task as soon as possible.
        Returns:
        true if task was cancelled before it completed
        See Also:
        cancel(boolean)
      • 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 when cancel is called, this task should never run. If the task has already started, then the mayInterruptIfRunning 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 after doInBackground(Object) returns. Calling this method guarantees that onPostExecute(Object) is never subsequently invoked, even if cancel returns false, but onPostExecute(com.pranavpandey.android.dynamic.util.concurrent.DynamicResult<R>) has not yet run. To finish the task as early as possible, check isCancelled() periodically from doInBackground(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:
        isCancelled(), onCancelled(DynamicResult)