Participe do evento ⁠#Android11: apresentação de lançamento da versão Beta no dia 3 de junho.

ActivityResultRegistry

public abstract class ActivityResultRegistry
extends Object

java.lang.Object
   ↳ androidx.activity.result.ActivityResultRegistry


A registry that stores activity result callbacks for registered calls. You can create your own instance for testing by overriding invoke(int, ActivityResultContract, I, ActivityOptionsCompat) and calling dispatchResult(int, O) immediately within it, thus skipping the actual Activity.startActivityForResult(Intent, int) call. When testing, make sure to explicitly provide a registry instance whenever calling ActivityResultCaller.registerForActivityResult(ActivityResultContract, ActivityResultCallback), to be able to inject a test instance.

Summary

Public constructors

ActivityResultRegistry()

Public methods

final boolean dispatchResult(int requestCode, int resultCode, Intent data)

Dispatch a result received via Activity.onActivityResult(int, int, Intent) to the callback on record, or store the result if callback was not yet registered.

final <O> boolean dispatchResult(int requestCode, O result)

Dispatch a result object to the callback on record.

abstract <I, O> void invoke(int requestCode, ActivityResultContract<I, O> contract, I input, ActivityOptionsCompat options)

Start the process of executing an ActivityResultContract in a type-safe way, using the provided contract.

final void onRestoreInstanceState(Bundle savedInstanceState)

Restore the state of this registry from the given Bundle

final void onSaveInstanceState(Bundle outState)

Save the state of this registry in the given Bundle

final <I, O> ActivityResultLauncher<I> register(String key, LifecycleOwner lifecycleOwner, ActivityResultContract<I, O> contract, ActivityResultCallback<O> callback)

Register a new callback with this registry.

final <I, O> ActivityResultLauncher<I> register(String key, ActivityResultContract<I, O> contract, ActivityResultCallback<O> callback)

Register a new callback with this registry.

Inherited methods

Public constructors

ActivityResultRegistry

public ActivityResultRegistry ()

Public methods

dispatchResult

public final boolean dispatchResult (int requestCode, 
                int resultCode, 
                Intent data)

Dispatch a result received via Activity.onActivityResult(int, int, Intent) to the callback on record, or store the result if callback was not yet registered.

Parameters
requestCode int: request code to identify the callback

resultCode int: status to indicate the success of the operation

data Intent: an intent that carries the result data

Returns
boolean whether there was a callback was registered for the given request code which was or will be called.

dispatchResult

public final boolean dispatchResult (int requestCode, 
                O result)

Dispatch a result object to the callback on record.

Parameters
requestCode int: request code to identify the callback

result O: the result to propagate

Returns
boolean true if there is a callback registered for the given request code, false otherwise.

invoke

public abstract void invoke (int requestCode, 
                ActivityResultContract<I, O> contract, 
                I input, 
                ActivityOptionsCompat options)

Start the process of executing an ActivityResultContract in a type-safe way, using the provided contract.

Parameters
requestCode int: request code to use

contract ActivityResultContract: contract to use for type conversions

input I: input required to execute an ActivityResultContract.

options ActivityOptionsCompat: Additional options for how the Activity should be started.

onRestoreInstanceState

public final void onRestoreInstanceState (Bundle savedInstanceState)

Restore the state of this registry from the given Bundle

Parameters
savedInstanceState Bundle: the place to restore from

onSaveInstanceState

public final void onSaveInstanceState (Bundle outState)

Save the state of this registry in the given Bundle

Parameters
outState Bundle: the place to put state into

register

public final ActivityResultLauncher<I> register (String key, 
                LifecycleOwner lifecycleOwner, 
                ActivityResultContract<I, O> contract, 
                ActivityResultCallback<O> callback)

Register a new callback with this registry. This is normally called by a higher level convenience methods like ActivityResultCaller.registerForActivityResult(ActivityResultContract, ActivityResultCallback).

Parameters
key String: a unique string key identifying this call

lifecycleOwner LifecycleOwner: a LifecycleOwner that makes this call.

contract ActivityResultContract: the contract specifying input/output types of the call

callback ActivityResultCallback: the activity result callback

Returns
ActivityResultLauncher<I> a launcher that can be used to execute an ActivityResultContract.

register

public final ActivityResultLauncher<I> register (String key, 
                ActivityResultContract<I, O> contract, 
                ActivityResultCallback<O> callback)

Register a new callback with this registry. This is normally called by a higher level convenience methods like ActivityResultCaller.registerForActivityResult(ActivityResultContract, ActivityResultCallback). When calling this, you must call ActivityResultLauncher.unregister() on the returned ActivityResultLauncher when the launcher is no longer needed to release any values that might be captured in the registered callback.

Parameters
key String: a unique string key identifying this call

contract ActivityResultContract: the contract specifying input/output types of the call

callback ActivityResultCallback: the activity result callback

Returns
ActivityResultLauncher<I> a launcher that can be used to execute an ActivityResultContract.