Task

Kotlin|Java

public abstract classTask<TResult>


Represents an asynchronous operation.

Parameters
<TResult>

the type of the result of the operation

See also
Tasks

Summary

Public constructors

Public methods

@NonNullTask<TResult>

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

@NonNullTask<TResult>

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

@NonNullTask<TResult>

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

@NonNullTask<TResult>

Adds a listener that is called when the Task completes.

@NonNullTask<TResult>

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

@NonNullTask<TResult>

Adds a listener that is called when the Task completes.

abstract @NonNullTask<TResult>

Adds a listener that is called if the Task fails.

abstract @NonNullTask<TResult>

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

abstract @NonNullTask<TResult>

Adds a listener that is called if the Task fails.

abstract @NonNullTask<TResult>

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

abstract @NonNullTask<TResult>

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

abstract @NonNullTask<TResult>

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

@NonNullTask<TContinuationResult>
<TContinuationResult>continueWith(
    @NonNullContinuation<TResult, TContinuationResult> continuation
)

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

@NonNullTask<TContinuationResult>
<TContinuationResult>continueWith(
    @NonNullExecutor executor,
    @NonNullContinuation<TResult, TContinuationResult> continuation
)

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

@NonNullTask<TContinuationResult>
<TContinuationResult>continueWithTask(
    @NonNullContinuation<TResult, Task<TContinuationResult>> continuation
)

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

@NonNullTask<TContinuationResult>
<TContinuationResult>continueWithTask(
    @NonNullExecutor executor,
    @NonNullContinuation<TResult, Task<TContinuationResult>> continuation
)

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

abstract @NullableException

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(@NonNullClass<X> exceptionType)

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

abstract boolean

Returnstrue if the Task is canceled;false otherwise.

abstract boolean

Returnstrue if the Task is complete;false otherwise.

abstract boolean

Returnstrue if the Task has completed successfully;false otherwise.

@NonNullTask<TContinuationResult>
<TContinuationResult>onSuccessTask(
    @NonNullSuccessContinuation<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.

@NonNullTask<TContinuationResult>
<TContinuationResult>onSuccessTask(
    @NonNullExecutor executor,
    @NonNullSuccessContinuation<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 @NonNullTask<TResult> addOnCanceledListener(@NonNullOnCanceledListener 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
@NonNullTask<TResult>

this Task

addOnCanceledListener

@CanIgnoreReturnValue
public @NonNullTask<TResult> addOnCanceledListener(
    @NonNullActivity activity,
    @NonNullOnCanceledListener 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 duringonStop.

Returns
@NonNullTask<TResult>

this Task

addOnCanceledListener

@CanIgnoreReturnValue
public @NonNullTask<TResult> addOnCanceledListener(
    @NonNullExecutor executor,
    @NonNullOnCanceledListener 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
@NonNullExecutor executor

the executor to use to call the listener

Returns
@NonNullTask<TResult>

this Task

addOnCompleteListener

@CanIgnoreReturnValue
public @NonNullTask<TResult> addOnCompleteListener(@NonNullOnCompleteListener<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
@NonNullTask<TResult>

this Task

addOnCompleteListener

@CanIgnoreReturnValue
public @NonNullTask<TResult> addOnCompleteListener(
    @NonNullActivity activity,
    @NonNullOnCompleteListener<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 duringonStop.

Returns
@NonNullTask<TResult>

this Task

addOnCompleteListener

@CanIgnoreReturnValue
public @NonNullTask<TResult> addOnCompleteListener(
    @NonNullExecutor executor,
    @NonNullOnCompleteListener<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
@NonNullExecutor executor

the executor to use to call the listener

Returns
@NonNullTask<TResult>

this Task

addOnFailureListener

@CanIgnoreReturnValue
public abstract @NonNullTask<TResult> addOnFailureListener(@NonNullOnFailureListener 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
@NonNullTask<TResult>

this Task

addOnFailureListener

@CanIgnoreReturnValue
public abstract @NonNullTask<TResult> addOnFailureListener(
    @NonNullActivity activity,
    @NonNullOnFailureListener 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 duringonStop.

Returns
@NonNullTask<TResult>

this Task

addOnFailureListener

@CanIgnoreReturnValue
public abstract @NonNullTask<TResult> addOnFailureListener(
    @NonNullExecutor executor,
    @NonNullOnFailureListener 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
@NonNullExecutor executor

the executor to use to call the listener

Returns
@NonNullTask<TResult>

this Task

addOnSuccessListener

@CanIgnoreReturnValue
public abstract @NonNullTask<TResult> addOnSuccessListener(@NonNullOnSuccessListener<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
@NonNullTask<TResult>

this Task

addOnSuccessListener

@CanIgnoreReturnValue
public abstract @NonNullTask<TResult> addOnSuccessListener(
    @NonNullActivity activity,
    @NonNullOnSuccessListener<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 duringonStop.

Returns
@NonNullTask<TResult>

this Task

addOnSuccessListener

@CanIgnoreReturnValue
public abstract @NonNullTask<TResult> addOnSuccessListener(
    @NonNullExecutor executor,
    @NonNullOnSuccessListener<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
@NonNullExecutor executor

the executor to use to call the listener

Returns
@NonNullTask<TResult>

this Task

continueWith

public @NonNullTask<TContinuationResult> <TContinuationResult>continueWith(
    @NonNullContinuation<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 @NonNullTask<TContinuationResult> <TContinuationResult>continueWith(
    @NonNullExecutor executor,
    @NonNullContinuation<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
@NonNullExecutor executor

the executor to use to call the Continuation

See also
then

continueWithTask

public @NonNullTask<TContinuationResult> <TContinuationResult>continueWithTask(
    @NonNullContinuation<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 @NonNullTask<TContinuationResult> <TContinuationResult>continueWithTask(
    @NonNullExecutor executor,
    @NonNullContinuation<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
@NonNullExecutor executor

the executor to use to call the Continuation

See also
then

getException

public abstract @NullableException getException()

Returns the exception that caused the Task to fail. Returnsnull 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(@NonNullClass<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()

Returnstrue if the Task is canceled;false otherwise.

isComplete

public abstract boolean isComplete()

Returnstrue if the Task is complete;false otherwise.

isSuccessful

public abstract boolean isSuccessful()

Returnstrue if the Task has completed successfully;false otherwise.

onSuccessTask

public @NonNullTask<TContinuationResult> <TContinuationResult>onSuccessTask(
    @NonNullSuccessContinuation<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 @NonNullTask<TContinuationResult> <TContinuationResult>onSuccessTask(
    @NonNullExecutor executor,
    @NonNullSuccessContinuation<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
@NonNullExecutor executor

the executor to use to call the SuccessContinuation

See also
then

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

Last updated 2025-03-17 UTC.