rememberLauncherForActivityResult

Functions summary

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

Register a request to start an activity for result, designated by the given ActivityResultContract.

Functions

rememberLauncherForActivityResult

@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 ActivityResultContract.

This creates a record in the registry associated with this caller, managing request code, as well as conversions to/from Intent under the hood.

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

You should not call ActivityResultLauncher.unregister on the returned ActivityResultLauncher. Attempting to do so will result in an IllegalStateException.

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

the contract, specifying conversions to/from Intents

onResult: (O) -> Unit

the callback to be called on the main thread when activity result is available

Returns
ManagedActivityResultLauncher<I, O>

the launcher that can be used to start the activity.