Mokera

Tech Sheets

mokelab

Launch an activity with a result in Jetpack Compose

Last updated:2024-07-10

In View System, we use registerForActivityResult() to launch an activity with a result.

We use rememberLauncherForActivityResult() for it in Jetpack Compose. Official document is here

@Composable
fun MainScreen() {
  var uri by remember { mutableStateOf<Uri?>(null) }
  val launcher = rememberLauncherForActivityResult(GetContent()) {
    uri = it
  }
  Column {
    Button(onClick = {
      launcher.launch("image/*")
    }) {
      Text("Pick Content")
    }
    if (uri != null) {
      Text("uri=${uri}")
    }
  }
}

Back