Google is committed to advancing racial equity for Black communities. See how.

ActivityResultRegistry

abstract class ActivityResultRegistry
kotlin.Any
   ↳ androidx.activity.result.ActivityResultRegistry

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

Summary

Public constructors

A registry that stores activity result callbacks for ActivityResultCaller#registerForActivityResult.

Public methods
Boolean
dispatchResult(requestCode: Int, resultCode: Int, @Nullable data: Intent?)

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

Boolean
dispatchResult(requestCode: Int, result: O)

Dispatch a result object to the callback on record.

abstract Unit
onLaunch(requestCode: Int, @NonNull contract: ActivityResultContract<I, O>, input: I, @Nullable options: ActivityOptionsCompat?)

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

Unit
onRestoreInstanceState(@Nullable savedInstanceState: Bundle?)

Restore the state of this registry from the given Bundle

Unit
onSaveInstanceState(@NonNull outState: Bundle)

Save the state of this registry in the given Bundle

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

Register a new callback with this registry.

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

Register a new callback with this registry.

Public constructors

<init>

ActivityResultRegistry()

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

Public methods

dispatchResult

@MainThread fun dispatchResult(
    requestCode: Int,
    resultCode: Int,
    @Nullable data: Intent?
): Boolean

Dispatch a result received via Activity#onActivityResult 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
Return
Boolean whether there was a callback was registered for the given request code which was or will be called.

dispatchResult

@MainThread fun <O : Any!> dispatchResult(
    requestCode: Int,
    result: O
): Boolean

Dispatch a result object to the callback on record.

Parameters
requestCode Int: request code to identify the callback
result O: the result to propagate
Return
Boolean true if there is a callback registered for the given request code, false otherwise.

onLaunch

@MainThread abstract fun <I : Any!, O : Any!> onLaunch(
    requestCode: Int,
    @NonNull contract: ActivityResultContract<I, O>,
    input: I,
    @Nullable options: ActivityOptionsCompat?
): Unit

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<I, O>: 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

fun onRestoreInstanceState(@Nullable savedInstanceState: Bundle?): Unit

Restore the state of this registry from the given Bundle

Parameters
savedInstanceState Bundle?: the place to restore from

onSaveInstanceState

fun onSaveInstanceState(@NonNull outState: Bundle): Unit

Save the state of this registry in the given Bundle

Parameters
outState Bundle: the place to put state into

register

@NonNull fun <I : Any!, O : Any!> register(
    @NonNull key: String,
    @NonNull lifecycleOwner: LifecycleOwner,
    @NonNull contract: ActivityResultContract<I, O>,
    @NonNull callback: ActivityResultCallback<O>
): ActivityResultLauncher<I>

Register a new callback with this registry. This is normally called by a higher level convenience methods like ActivityResultCaller#registerForActivityResult.

Parameters
key String: a unique string key identifying this call
lifecycleOwner LifecycleOwner: a LifecycleOwner that makes this call.
contract ActivityResultContract<I, O>: the contract specifying input/output types of the call
callback ActivityResultCallback<O>: the activity result callback
Return
ActivityResultLauncher<I> a launcher that can be used to execute an ActivityResultContract.

register

@NonNull fun <I : Any!, O : Any!> register(
    @NonNull key: String,
    @NonNull contract: ActivityResultContract<I, O>,
    @NonNull callback: ActivityResultCallback<O>
): ActivityResultLauncher<I>

Register a new callback with this registry. This is normally called by a higher level convenience methods like ActivityResultCaller#registerForActivityResult. 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<I, O>: the contract specifying input/output types of the call
callback ActivityResultCallback<O>: the activity result callback
Return
ActivityResultLauncher<I> a launcher that can be used to execute an ActivityResultContract.