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

@NonNull ActivityResultContract<@NonNull I, @NonNull O> contract

contract to use for type conversions

@NonNull I input

input required to execute an ActivityResultContract.

@Nullable ActivityOptionsCompat options

Additional options for how the Activity should be started.

onRestoreInstanceState

@NonNull
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

@NonNull
public final void onSaveInstanceState(@NonNull Bundle outState)

Save the state of this registry in the given Bundle

Parameters
@NonNull Bundle outState

the place to put state into

register

@NonNull
public final 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. This is normally called by a higher level convenience methods like registerForActivityResult.

Parameters
@NonNull String key

a unique string key identifying this call

@NonNull LifecycleOwner lifecycleOwner

a LifecycleOwner that makes this call.

@NonNull ActivityResultContract<@NonNull I, @NonNull O> contract

the contract specifying input/output types of the call

@NonNull ActivityResultCallback<@NonNull O> callback

the activity result callback

Returns
ActivityResultLauncher<@NonNull I>

a launcher that can be used to execute an ActivityResultContract.

register

@NonNull
public final 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. This is normally called by a higher level convenience methods like registerForActivityResult. When calling this, you must call 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
@NonNull String key

a unique string key identifying this call

@NonNull ActivityResultContract<@NonNull I, @NonNull O> contract

the contract specifying input/output types of the call

@NonNull ActivityResultCallback<@NonNull O> callback

the activity result callback

Returns
ActivityResultLauncher<@NonNull I>

a launcher that can be used to execute an ActivityResultContract.