Tech Sheets

Launch an activity with a result in Jetpack Compose

最終更新日: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}")
    }
  }
}

一覧に戻る