getting back a result from LauncherForActivityResult

372 Views Asked by At

I'm a bit stuck in a function I'm writing.

I have a function that calls another app I wrote and needs to return a Boolean

@Composable
fun callApp(
    //someparams
) : Boolean {
      //some code
}

after some logic, i launch my application via:

val startForResult =
        rememberLauncherForActivityResult(ActivityResultContracts.StartActivityForResult()) { result: ActivityResult ->
            if (result.resultCode == Activity.RESULT_OK) {
               //here i need to set the return value of "callApp"
            }}

And trigger it by

startForResult.launch(launchIntent)

The problem is that i can't see a way in which i can return the boolean generated in the result by the function "callApp". Note that i can't use sharedPref/realm/any data structure external from code itself.

Can you help me understand this?

1

There are 1 best solutions below

0
On BEST ANSWER

It best to just make a callback for intent completion instead of returning value from composable/storing it somewhere

@Composable
fun callApp(
    //someparams,
    onIntentFinished: (Boolean) -> Unit
) {
    // ...
    val startForResult =
        rememberLauncherForActivityResult(ActivityResultContracts.StartActivityForResult()) { result: ActivityResult ->
            if (result.resultCode == Activity.RESULT_OK) {
               //here i need to set the return value of "callApp"
               onIntentFinished(true)
            }}
    // ...
}

For other solutions, you might want to pass down an object to store the value