# rememberLauncherForActivityResult

## Functions summary

|---|---|
| `https://developer.android.com/reference/kotlin/androidx/activity/compose/ManagedActivityResultLauncher<I, O>` | `@https://developer.android.com/reference/kotlin/androidx/compose/runtime/Composable <I : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?, O : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://developer.android.com/reference/kotlin/androidx/activity/compose/rememberLauncherForActivityResult.composable#rememberLauncherForActivityResult(androidx.activity.result.contract.ActivityResultContract,kotlin.Function1)( contract: https://developer.android.com/reference/kotlin/androidx/activity/result/contract/ActivityResultContract<I, O>, onResult: (O) -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html )` Register a request to start an activity for result, designated by the given `https://developer.android.com/reference/kotlin/androidx/activity/compose/package-summary#rememberLauncherForActivityResult(androidx.activity.result.contract.ActivityResultContract,kotlin.Function1)`. |

## Functions

### rememberLauncherForActivityResult

Artifact: [androidx.activity:activity-compose](https://developer.android.com/jetpack/androidx/releases/activity) [View Source](https://cs.android.com/search?q=file:androidx/activity/compose/ActivityResultRegistry.kt+function:rememberLauncherForActivityResult) Added in [1.3.0](https://developer.android.com/jetpack/androidx/releases/activity#1.3.0)

```
@Composable
fun <I : Any?, O : Any?> rememberLauncherForActivityResult(
    contract: ActivityResultContract<I, O>,
    onResult: (O) -> Unit
): ManagedActivityResultLauncher<I, O>
```

Register a request to start an activity for result, designated by the given `https://developer.android.com/reference/kotlin/androidx/activity/compose/package-summary#rememberLauncherForActivityResult(androidx.activity.result.contract.ActivityResultContract,kotlin.Function1)`.

This creates a record in the `https://developer.android.com/reference/kotlin/androidx/activity/result/ActivityResultRegistry` associated with this caller, managing request code, as well as conversions to/from `https://developer.android.com/reference/android/content/Intent.html` under the hood.

This *must* be called unconditionally, as part of initialization path.

You should *not* call `https://developer.android.com/reference/kotlin/androidx/activity/result/ActivityResultLauncher#unregister()` on the returned `https://developer.android.com/reference/kotlin/androidx/activity/result/ActivityResultLauncher`. Attempting to do so will result in an `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-state-exception/index.html`.

```kotlin
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.result.launch
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.asImageBitmap

val result = remember { mutableStateOf<Bitmap?>(null) }
val launcher =
    rememberLauncherForActivityResult(ActivityResultContracts.TakePicturePreview()) {
        result.value = it
    }

Button(onClick = { launcher.launch() }) { Text(text = "Take a picture") }

result.value?.let { image ->
    Image(image.asImageBitmap(), null, modifier = Modifier.fillMaxWidth())
}
```

| Parameters |
|---|---|
| `contract: https://developer.android.com/reference/kotlin/androidx/activity/result/contract/ActivityResultContract<I, O>` | the contract, specifying conversions to/from `https://developer.android.com/reference/android/content/Intent.html`s |
| `onResult: (O) -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | the callback to be called on the main thread when activity result is available |

| Returns |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/activity/compose/ManagedActivityResultLauncher<I, O>` | the launcher that can be used to start the activity. |