KotlinLeaner
08/18/2022, 11:48 AMonActivityResult()
.
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.KotlinLeaner
08/18/2022, 11:49 AMval 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
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
compose version 1.1.1
Ian Lake
08/18/2022, 2:16 PMKotlinLeaner
08/18/2022, 2:19 PMIan Lake
08/18/2022, 2:19 PMKotlinLeaner
08/18/2022, 2:20 PMIan Lake
08/18/2022, 2:21 PMKotlinLeaner
08/18/2022, 2:23 PMKotlinLeaner
08/18/2022, 2:24 PMKotlinLeaner
08/18/2022, 2:24 PMKotlinLeaner
08/18/2022, 2:26 PMActivity 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.Ian Lake
08/18/2022, 2:30 PMKotlinLeaner
08/18/2022, 2:34 PMLaunchInputActivity
Ian Lake
08/18/2022, 2:35 PMKotlinLeaner
08/18/2022, 2:36 PMfun Context.findActivity(): AppCompatActivity? = when (this) {
is AppCompatActivity -> this
is ContextWrapper -> baseContext.findActivity()
else -> null
}
And set this in my composable function
val activity = context.findActivity()
activity?.let {
it.setResult(Activity.RESULT_OK)
it.finish()
}
is this correct?KotlinLeaner
08/18/2022, 2:37 PMIan Lake
08/18/2022, 2:47 PMKotlinLeaner
08/18/2022, 2:53 PM