ActivityResultRegistry


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

Public functions

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

Dispatch a result object to the callback on record.

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

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

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

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

Unit
onRestoreInstanceState(savedInstanceState: Bundle?)

Restore the state of this registry from the given Bundle

Unit

Save the state of this registry in the given Bundle

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

Register a new callback with this registry.

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

Register a new callback with this registry.

Public constructors

ActivityResultRegistry

Added in 1.2.0
ActivityResultRegistry()

Public functions

dispatchResult

Added in 1.2.0
@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

Returns
Boolean

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

dispatchResult

Added in 1.2.0
@MainThread
fun dispatchResult(requestCode: Int, resultCode: Int, 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

Returns
Boolean

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

onLaunch

Added in 1.2.0
@MainThread
abstract fun <I : Any?, O : Any?> onLaunch(
    requestCode: Int,
    contract: ActivityResultContract<I, O>,
    input: I,
    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

Added in 1.2.0
fun onRestoreInstanceState(savedInstanceState: Bundle?): Unit

Restore the state of this registry from the given Bundle

Parameters
savedInstanceState: Bundle?

the place to restore from

onSaveInstanceState

Added in 1.2.0
fun onSaveInstanceState(outState: Bundle): Unit

Save the state of this registry in the given Bundle

Parameters
outState: Bundle

the place to put state into

register

Added in 1.2.0
fun <I : Any?, O : Any?> register(
    key: String,
    contract: ActivityResultContract<I, O>,
    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

Returns
ActivityResultLauncher<I>

a launcher that can be used to execute an ActivityResultContract.

register

Added in 1.2.0
fun <I : Any?, O : Any?> register(
    key: String,
    lifecycleOwner: LifecycleOwner,
    contract: ActivityResultContract<I, O>,
    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

Returns
ActivityResultLauncher<I>

a launcher that can be used to execute an ActivityResultContract.