Task

public abstract class Task<TResult>


Represents an asynchronous operation.

Parameters
<TResult>

the type of the result of the operation

See also
Tasks

Summary

Public constructors

Public methods

@NonNull Task<TResult>

Adds a listener that is called if the Task is canceled.

@NonNull Task<TResult>

Adds an Activity-scoped listener that is called if the Task is canceled.

@NonNull Task<TResult>

Adds a listener that is called if the Task is canceled.

@NonNull Task<TResult>

Adds a listener that is called when the Task completes.

@NonNull Task<TResult>

Adds an Activity-scoped listener that is called when the Task completes.

@NonNull Task<TResult>

Adds a listener that is called when the Task completes.

abstract @NonNull Task<TResult>

Adds a listener that is called if the Task fails.

abstract @NonNull Task<TResult>

Adds an Activity-scoped listener that is called if the Task fails.

abstract @NonNull Task<TResult>

Adds a listener that is called if the Task fails.

abstract @NonNull Task<TResult>

Adds a listener that is called if the Task completes successfully.

abstract @NonNull Task<TResult>

Adds an Activity-scoped listener that is called if the Task completes successfully.

abstract @NonNull Task<TResult>

Adds a listener that is called if the Task completes successfully.

@NonNull Task<TContinuationResult>
<TContinuationResult> continueWith(
    @NonNull Continuation<TResult, TContinuationResult> continuation
)

Returns a new Task that will be completed with the result of applying the specified Continuation to this Task.

@NonNull Task<TContinuationResult>
<TContinuationResult> continueWith(
    @NonNull Executor executor,
    @NonNull Continuation<TResult, TContinuationResult> continuation
)

Returns a new Task that will be completed with the result of applying the specified Continuation to this Task.

@NonNull Task<TContinuationResult>
<TContinuationResult> continueWithTask(
    @NonNull Continuation<TResult, Task<TContinuationResult>> continuation
)

Returns a new Task that will be completed with the result of applying the specified Continuation to this Task.

@NonNull Task<TContinuationResult>
<TContinuationResult> continueWithTask(
    @NonNull Executor executor,
    @NonNull Continuation<TResult, Task<TContinuationResult>> continuation
)

Returns a new Task that will be completed with the result of applying the specified Continuation to this Task.

abstract @Nullable Exception

Returns the exception that caused the Task to fail.

abstract TResult

Gets the result of the Task, if it has already completed.

abstract TResult
<X extends Throwable> getResult(@NonNull Class<X> exceptionType)

Gets the result of the Task, if it has already completed.

abstract boolean

Returns true if the Task is canceled; false otherwise.

abstract boolean

Returns true if the Task is complete; false otherwise.

abstract boolean

Returns true if the Task has completed successfully; false otherwise.

@NonNull Task<TContinuationResult>
<TContinuationResult> onSuccessTask(
    @NonNull SuccessContinuation<TResult, TContinuationResult> successContinuation
)

Returns a new Task that will be completed with the result of applying the specified SuccessContinuation to this Task when this Task completes successfully.

@NonNull Task<TContinuationResult>
<TContinuationResult> onSuccessTask(
    @NonNull Executor executor,
    @NonNull SuccessContinuation<TResult, TContinuationResult> successContinuation
)

Returns a new Task that will be completed with the result of applying the specified SuccessContinuation to this Task when this Task completes successfully.

Public constructors

Task

public Task()

Public methods

addOnCanceledListener

@CanIgnoreReturnValue
public @NonNull Task<TResult> addOnCanceledListener(@NonNull OnCanceledListener listener)

Adds a listener that is called if the Task is canceled.

The listener will be called on main application thread. If the Task has already been canceled, a call to the listener will be immediately scheduled. If multiple listeners are added, they will be called in the order in which they were added.

Returns
@NonNull Task<TResult>

this Task

addOnCanceledListener

@CanIgnoreReturnValue
public @NonNull Task<TResult> addOnCanceledListener(
    @NonNull Activity activity,
    @NonNull OnCanceledListener listener
)

Adds an Activity-scoped listener that is called if the Task is canceled.

The listener will be called on main application thread. If the Task has already been canceled, a call to the listener will be immediately scheduled. If multiple listeners are added, they will be called in the order in which they were added.

The listener will be automatically removed during onStop.

Returns
@NonNull Task<TResult>

this Task

addOnCanceledListener

@CanIgnoreReturnValue
public @NonNull Task<TResult> addOnCanceledListener(
    @NonNull Executor executor,
    @NonNull OnCanceledListener listener
)

Adds a listener that is called if the Task is canceled.

If the Task has already been canceled, a call to the listener will be immediately scheduled. If multiple listeners are added, they will be called in the order in which they were added.

Parameters
@NonNull Executor executor

the executor to use to call the listener

Returns
@NonNull Task<TResult>

this Task

addOnCompleteListener

@CanIgnoreReturnValue
public @NonNull Task<TResult> addOnCompleteListener(@NonNull OnCompleteListener<TResult> listener)

Adds a listener that is called when the Task completes.

The listener will be called on main application thread. If the Task is already complete, a call to the listener will be immediately scheduled. A Task is considered complete if it succeeds, fails, or is canceled. If multiple listeners are added, they will be called in the order in which they were added.

Returns
@NonNull Task<TResult>

this Task

addOnCompleteListener

@CanIgnoreReturnValue
public @NonNull Task<TResult> addOnCompleteListener(
    @NonNull Activity activity,
    @NonNull OnCompleteListener<TResult> listener
)

Adds an Activity-scoped listener that is called when the Task completes.

The listener will be called on main application thread. If the Task is already complete, a call to the listener will be immediately scheduled. A Task is considered complete if it succeeds, fails, or is canceled. If multiple listeners are added, they will be called in the order in which they were added.

The listener will be automatically removed during onStop.

Returns
@NonNull Task<TResult>

this Task

addOnCompleteListener

@CanIgnoreReturnValue
public @NonNull Task<TResult> addOnCompleteListener(
    @NonNull Executor executor,
    @NonNull OnCompleteListener<TResult> listener
)

Adds a listener that is called when the Task completes.

If the Task is already complete, a call to the listener will be immediately scheduled. A Task is considered complete if it succeeds, fails, or is canceled. If multiple listeners are added, they will be called in the order in which they were added.

Parameters
@NonNull Executor executor

the executor to use to call the listener

Returns
@NonNull Task<TResult>

this Task

addOnFailureListener

@CanIgnoreReturnValue
public abstract @NonNull Task<TResult> addOnFailureListener(@NonNull OnFailureListener listener)

Adds a listener that is called if the Task fails.

The listener will be called on main application thread. If the Task has already failed, a call to the listener will be immediately scheduled. If multiple listeners are added, they will be called in the order in which they were added.

A canceled Task is not a failure Task. This listener will not trigger if the Task is canceled.

Returns
@NonNull Task<TResult>

this Task

addOnFailureListener

@CanIgnoreReturnValue
public abstract @NonNull Task<TResult> addOnFailureListener(
    @NonNull Activity activity,
    @NonNull OnFailureListener listener
)

Adds an Activity-scoped listener that is called if the Task fails.

The listener will be called on main application thread. If the Task has already failed, a call to the listener will be immediately scheduled. If multiple listeners are added, they will be called in the order in which they were added.

A canceled Task is not a failure Task. This listener will not trigger if the Task is canceled.

The listener will be automatically removed during onStop.

Returns
@NonNull Task<TResult>

this Task

addOnFailureListener

@CanIgnoreReturnValue
public abstract @NonNull Task<TResult> addOnFailureListener(
    @NonNull Executor executor,
    @NonNull OnFailureListener listener
)

Adds a listener that is called if the Task fails.

If the Task has already failed, a call to the listener will be immediately scheduled. If multiple listeners are added, they will be called in the order in which they were added.

Parameters
@NonNull Executor executor

the executor to use to call the listener

Returns
@NonNull Task<TResult>

this Task

addOnSuccessListener

@CanIgnoreReturnValue
public abstract @NonNull Task<TResult> addOnSuccessListener(@NonNull OnSuccessListener<Object> listener)

Adds a listener that is called if the Task completes successfully.

The listener will be called on the main application thread. If the Task has already completed successfully, a call to the listener will be immediately scheduled. If multiple listeners are added, they will be called in the order in which they were added.

Returns
@NonNull Task<TResult>

this Task

addOnSuccessListener

@CanIgnoreReturnValue
public abstract @NonNull Task<TResult> addOnSuccessListener(
    @NonNull Activity activity,
    @NonNull OnSuccessListener<Object> listener
)

Adds an Activity-scoped listener that is called if the Task completes successfully.

The listener will be called on the main application thread. If the Task has already completed successfully, a call to the listener will be immediately scheduled. If multiple listeners are added, they will be called in the order in which they were added.

The listener will be automatically removed during onStop.

Returns
@NonNull Task<TResult>

this Task

addOnSuccessListener

@CanIgnoreReturnValue
public abstract @NonNull Task<TResult> addOnSuccessListener(
    @NonNull Executor executor,
    @NonNull OnSuccessListener<Object> listener
)

Adds a listener that is called if the Task completes successfully.

If multiple listeners are added, they will be called in the order in which they were added. If the Task has already completed successfully, a call to the listener will be immediately scheduled.

Parameters
@NonNull Executor executor

the executor to use to call the listener

Returns
@NonNull Task<TResult>

this Task

continueWith

public @NonNull Task<TContinuationResult> <TContinuationResult> continueWith(
    @NonNull Continuation<TResult, TContinuationResult> continuation
)

Returns a new Task that will be completed with the result of applying the specified Continuation to this Task.

The Continuation will be called on the main application thread.

If the previous Task is canceled, the returned Task will also be canceled and the Continuation would not execute.

See also
then

continueWith

public @NonNull Task<TContinuationResult> <TContinuationResult> continueWith(
    @NonNull Executor executor,
    @NonNull Continuation<TResult, TContinuationResult> continuation
)

Returns a new Task that will be completed with the result of applying the specified Continuation to this Task.

If the previous Task is canceled, the returned Task will also be canceled and the Continuation would not execute.

Parameters
@NonNull Executor executor

the executor to use to call the Continuation

See also
then

continueWithTask

public @NonNull Task<TContinuationResult> <TContinuationResult> continueWithTask(
    @NonNull Continuation<TResult, Task<TContinuationResult>> continuation
)

Returns a new Task that will be completed with the result of applying the specified Continuation to this Task.

The Continuation will be called on the main application thread.

If the previous Task is canceled, the Continuation would still execute and task.isCanceled() is true can be observed in the Continuation.

See also
then

continueWithTask

public @NonNull Task<TContinuationResult> <TContinuationResult> continueWithTask(
    @NonNull Executor executor,
    @NonNull Continuation<TResult, Task<TContinuationResult>> continuation
)

Returns a new Task that will be completed with the result of applying the specified Continuation to this Task.

If the previous Task is canceled, the Continuation would still execute and task.isCanceled() is true can be observed in the Continuation.

Parameters
@NonNull Executor executor

the executor to use to call the Continuation

See also
then

getException

public abstract @Nullable Exception getException()

Returns the exception that caused the Task to fail. Returns null if the Task is not yet complete, or completed successfully.

getResult

public abstract TResult getResult()

Gets the result of the Task, if it has already completed.

Throws
java.lang.IllegalStateException

if the Task is not yet complete

com.google.android.gms.tasks.RuntimeExecutionException

if the Task failed with an exception

getResult

public abstract TResult <X extends Throwable> getResult(@NonNull Class<X> exceptionType)

Gets the result of the Task, if it has already completed.

Throws
java.lang.IllegalStateException

if the Task is not yet complete

X

if the Task failed with an exception of type X

com.google.android.gms.tasks.RuntimeExecutionException

if the Task failed with an exception that was not of type X

isCanceled

public abstract boolean isCanceled()

Returns true if the Task is canceled; false otherwise.

isComplete

public abstract boolean isComplete()

Returns true if the Task is complete; false otherwise.

isSuccessful

public abstract boolean isSuccessful()

Returns true if the Task has completed successfully; false otherwise.

onSuccessTask

public @NonNull Task<TContinuationResult> <TContinuationResult> onSuccessTask(
    @NonNull SuccessContinuation<TResult, TContinuationResult> successContinuation
)

Returns a new Task that will be completed with the result of applying the specified SuccessContinuation to this Task when this Task completes successfully. If the previous Task fails, the onSuccessTask completion will be skipped and failure listeners will be invoked.

The SuccessContinuation will be called on the main application thread.

If the previous Task is canceled, the returned Task will also be canceled and the SuccessContinuation would not execute.

See also
then

onSuccessTask

public @NonNull Task<TContinuationResult> <TContinuationResult> onSuccessTask(
    @NonNull Executor executor,
    @NonNull SuccessContinuation<TResult, TContinuationResult> successContinuation
)

Returns a new Task that will be completed with the result of applying the specified SuccessContinuation to this Task when this Task completes successfully. If the previous Task fails, the onSuccessTask completion will be skipped and failure listeners will be invoked.

If the previous Task is canceled, the returned Task will also be canceled and the SuccessContinuation would not execute.

Parameters
@NonNull Executor executor

the executor to use to call the SuccessContinuation

See also
then