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 methods

final @MainThread @NonNull boolean
dispatchResult(
    @NonNull int requestCode,
    @NonNull 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 @NonNull boolean
<O> dispatchResult(@NonNull int requestCode, @NonNull O result)

Dispatch a result object to the callback on record.

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

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

final @NonNull void
onRestoreInstanceState(@Nullable Bundle savedInstanceState)

Restore the state of this registry from the given Bundle

final @NonNull void

Save the state of this registry in the given Bundle

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

Register a new callback with this registry.

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

Register a new callback with this registry.

Public methods

dispatchResult

@MainThread
@NonNull
public final @MainThread boolean dispatchResult(
    @NonNull int requestCode,
    @NonNull 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
@NonNull int requestCode

request code to identify the callback

@NonNull int resultCode

status to indicate the success of the operation

@Nullable Intent data

an intent that carries the result data

Returns
@MainThread boolean

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

dispatchResult

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

Dispatch a result object to the callback on record.

Parameters
@NonNull int requestCode

request code to identify the callback

@NonNull O result

the result to propagate

Returns
@MainThread boolean

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

onLaunch

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

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

Parameters
@NonNull int requestCode

request code to use