ActivityResultRegistry

public abstract class ActivityResultRegistry


A registry that stores activity result callbacks for registered calls. You can create your own instance for testing by overriding onLaunch and calling dispatchResult immediately within it, thus skipping the actual startActivityForResult call. When testing, make sure to explicitly provide a registry instance whenever calling registerForActivityResult, to be able to inject a test instance.

Summary

Public constructors

Public methods

final @MainThread boolean
dispatchResult(int requestCode, int resultCode, @Nullable Intent data)

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

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

Dispatch a result object to the callback on record.

abstract @MainThread void
<I, O> onLaunch(
    int requestCode,
    @NonNull ActivityResultContract<I, O> contract,
    I input,
    @Nullable ActivityOptionsCompat options
)

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

final void
onRestoreInstanceState(@Nullable Bundle savedInstanceState)

Restore the state of this registry from the given Bundle

final void

Save the state of this registry in the given Bundle

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

Register a new callback with this registry.

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

Register a new callback with this registry.

Public constructors

ActivityResultRegistry

public final ActivityResultRegistry()

Public methods

dispatchResult

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

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

Parameters
int requestCode

request code to identify the callback

int resultCode

status to indicate the success of the operation

@Nullable Intent data

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

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

Dispatch a result object to the callback on record.

Parameters
int requestCode

request code to identify the callback

O result

the result to propagate

Returns
boolean

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

onLaunch

@MainThread
public abstract void <I, O> onLaunch(
    int requestCode,
    @NonNull ActivityResultContract<I, O> contract,
    I input,
    @Nullable ActivityOptionsCompat options
)

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

Parameters
int requestCode

request code to use

@NonNull ActivityResultContract<I, O> contract

contract to use for type conversions

I input

input required to execute an ActivityResultContract.

@Nullable ActivityOptionsCompat options

Additional options for how the Activity should be started.

onRestoreInstanceState

public final void onRestoreInstanceState(@Nullable Bundle savedInstanceState)

Restore the state of this registry from the given Bundle

Parameters
@Nullable Bundle savedInstanceState

the place to restore from

onSaveInstanceState

public final void onSaveInstanceState(@NonNull Bundle outState)

Save the state of this registry in the given