I want to open my Activity A -&gt; Activity B with `onActivityResult()`. 1. I am reading this <https...
k
I want to open my Activity A -> Activity B with
onActivityResult()
. 1. I am reading this stack overflow they are using LaunchEffect. So should I use this in my composable function. 2. I see @ianhanniballake stackoverflow using DisposableEffect so what's the use of this? 3. In normal xml activity we set
setResult(RESULT_OK, intent)
on Activity B when we want to submit something. So what do we use in compose function? Now I am adding my code.
xml activity
Copy code
val startInputForResult = registerForActivityResult(StartActivityForResult()) { result ->
        if (result.resultCode == Activity.RESULT_OK) {
            val data = result.data
            logE("data $data")
        }
    }

Input.setOnClickListener {
      startInputForResult.launch(launchInput(this, item))    
}
LaunchInputActivity which is fully composable
Copy code
class LaunchInputActivity : ComponentActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            Theme {
                AppBarScaffold() {
                        Input()
                }
            }
        }
    }
}

@Composable
fun Input(
    viewModel: InputViewModel = getViewModel()
) {
    Column(
        modifier = Modifier
            .fillMaxHeight()
            .verticalScroll(rememberScrollState())
            .padding(10.dp),
        verticalArrangement = Arrangement.SpaceBetween
    ) {
       Button (){
           setResult() // giving error to me unknown reference.
       }
    }
}
can someone guide me. How can I use this in project? Thanks
Copy code
compose version 1.1.1
i
You'd want to follow the official docs for Activity Results in Compose: https://developer.android.com/jetpack/compose/libraries#activity_result
k
This example shows that calling one composable function to another compassable. It nothing mention to normal Interoperability with composable activity
i
The setResult side on the other activity hasn't changed at all ever, Compose or not - you still need a reference to the activity to call setResult
k
Okk little bit clear
i
Maybe it would help to focus your question on which side you actually have a question on because both of the posts you link are only on the starting side, but your question seems to be about the receiving side?
k
My mainly question is how can we open composable activity from normal activity.
When user sumbit on composable activity. How can I received back data in my normal activity.
Now Is it clear to you ?
I'll explain in detail
Activity A
wants to open
Activity B
. When user submit data on
Activity B
then it returns to
Activity A
.
Activity A
means normal xml layout.
Activity B
means fully composable activity.
i
Okay, so just the normal Activity Result APIs on the starting side, that part it looks like you got correct already.
k
If you need code my composable activity. I attached the above snippet. Please have a look on
LaunchInputActivity
i
setResult is a method on Activity, so you need a reference to your Activity, like I mentioned above. So now you have two choices: • your Input composable can find the Activity itself (say, from LocalContext.current, casting it to an Activity) and call setResult on the activity • Your Input composable could take a lambda method as an input, which your Activity then uses to call setResult on itself using whatever data Input passed to it via the lambda
k
I tried this piece of code
Copy code
fun Context.findActivity(): AppCompatActivity? = when (this) {
    is AppCompatActivity -> this
    is ContextWrapper -> baseContext.findActivity()
    else -> null
}
And set this in my composable function
Copy code
val activity = context.findActivity()
activity?.let {
    it.setResult(Activity.RESULT_OK)
    it.finish()
}
is this correct?
I really appreciate your advice.. Thanks
i
Yep, that would do it
k
👍