Do we know how to get the activity result back to ...
# compose
d
Do we know how to get the activity result back to the Composable. I'm opening an activity using an intent where i'm selecting a file. I wan to get that in back to the composable. Do we have something like onActivityResult
p
why don’t you handle it in business logic, e.g. emitting an event/action when result is returned to Activity (or using inline library to to get rid of the callback) and then recompose the ui on state change?
d
Actually i'm launching a intent from composable itself.
Copy code
@Composable
fun AttachmentComponent() {

    val context = LocalContext.current
    val file = File(Environment.getExternalStorageDirectory().absolutePath + "/" + "deepak")

    fun openFileExplorer() {
        val target = Intent(Intent.ACTION_GET_CONTENT)
        target.setDataAndType(Uri.fromFile(file),"application/pdf");
        target.flags = Intent.FLAG_ACTIVITY_NO_HISTORY;
        val intent = Intent.createChooser(target, "Open File")
        context.startActivity(intent)
    }

    Column {
        Text(text = "Attachment")
        OutlinedButton(
            onClick = { openFileExplorer() }
        ) {
            Icon(painterResource(id = R.drawable.ico_attach), contentDescription = "attach")
            Text(text = "Add Files")
        }
    }
}
the challenge that i'm facing is that, how do i get back the result(the file that user has selected) in this composable function
s
You can use the new ActivityResult apis. There's even a new composable
registerForActivityResult
function available for that purpose.
🙌 1
i
Yep, that API was added in yesterday's Activity Compose 1.3.0-alpha03: https://developer.android.com/jetpack/androidx/releases/activity#1.3.0-alpha03
🙌 2
d
Thank you all it is great help.