https://kotlinlang.org logo
#compose
Title
# compose
n

nrobi

12/03/2020, 12:42 PM
Suppose I want to call
@Composable registerForActivityResult()
from the callback of a
clickable()
. Are there any options/workarounds to call it directly from the callback?
i

Ian Lake

12/03/2020, 3:07 PM
As per the Activity Result docs (https://developer.android.com/training/basics/intents/result#register), you must register unconditionally earlier to receive results after process death and recreation / config changes
We've talked about Activity Result before though (and have a gist that does exactly what you need): https://kotlinlang.slack.com/archives/CJLTWPH7S/p1605830823419600?thread_ts=1605820747.415200&cid=CJLTWPH7S
n

nrobi

12/03/2020, 3:13 PM
@Ian Lake Yupp, sorry for the vagueness of the description. I’ve seen the gist and I’m currently using it. My specific issue is, that I’m trying to call a composable function from a non-composable (
onClick: () -> Unit
)
So more specifically I’m looking for a way to do this:
Copy code
Modifier.clickable { GetImage(viewModel::onIconChanged) }
where
Copy code
@Composable
fun GetImage(callback: (Uri) -> Unit){}
Without the overhead of having a state flag or some similar implementation
i

Ian Lake

12/03/2020, 4:44 PM
That's not something you can do with Activity Results - your app can (and will, particularly when calling the camera on any medium/low ram device) get kicked out of memory, etc. when the other activity comes up and before your result comes back
So no callback based approach will ever be resilient enough to work
4 Views