public abstract class Task<TResult>

Represents an asynchronous operation.

Parameters
<TResult>

the type of the result of the operation

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

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.

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.

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.

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.

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

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.

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.

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.

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.

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.

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

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.

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

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.

getResult

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

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

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.

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

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-03-17 UTC.