A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://developers.google.com/android/reference/com/google/android/gms/tasks/CancellationToken below:

CancellationToken | Google Play services

Stay organized with collections Save and categorize content based on your preferences.

public abstract class CancellationToken

Propagates notification that operations should be canceled.

Developers writing methods that return a Task should take a CancellationToken as a parameter if they wish to make the Task cancelable (see below code snippet). A CancellationToken can only be created by creating a new instance of . CancellationToken is immutable and must be canceled by calling cancel on the CancellationTokenSource that creates it. It can only be canceled once. If canceled, it should not be passed to future operations.

When cancel is called, all the Tasks with the CancellationToken from that CancellationTokenSource will be canceled. This operation only flags those Tasks as canceled, and the API author is responsible for stopping whatever the Task is actually doing to free up the resources.

Cancellable Task example:

public Task<Integer> doSomething(CancellationToken token) {

  // Attach a listener that will be called once cancellation is requested.
  token.onCanceledRequested(new OnTokenCanceledListener() {
    @Override
    public void onCanceled() {
      // Some other operations to cancel this Task, such as free resources...
    }
  });

  final TaskCompletionSource<Integer> tcs = new TaskCompletionSource<>(token);

  // do something...

}

CancellationTokenSource cts = new CancellationTokenSource();
Task<Integer> task = doSomething(cts.getToken());
cts.cancel();

Cancellable

Task

example in

android.app.Activity

context:

public class MyActivity extends Activity {
  // ...

  @Override
  public void onStart() {
    super.onStart();

    // Typically use one cancellation source per lifecycle.
    cancellationSource = new CancellationTokenSource();

    // That source's token can be passed to multiple calls.
    doSomethingCancellable(cancellationSource.getToken())
      .onSuccessTask(result -> doSomethingElse(result, cancellationSource.getToken()));
  }

  @Override
  public void onStop() {
    super.onStop();
    cancellationSource.cancel();
  }
}
Summary

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 2024-10-31 UTC.

[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-10-31 UTC."],[[["`CancellationToken` signals cancellation requests to cancellable tasks."],["Create a `CancellationToken` using `CancellationTokenSource` and pass it to tasks for cancellation support."],["Upon cancellation, tasks with the associated token are flagged, requiring the API author to handle resource cleanup."],["Utilize `onCanceledRequested` to attach listeners that execute upon cancellation."],["`isCancellationRequested` helps determine if a cancellation request has been made."]]],[]]


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4