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

galex

07/23/2020, 12:41 PM
Is there an example on how to use the new Activity Results API with Compose? I'd like to do a Google sign-in from a Composable and would rather not pass down something from the Activity all the way down to the Composable
j

Joost Klitsie

07/23/2020, 12:43 PM
Did you try to get the current context, cast it to activity and registerForActivityResult on that one?
g

galex

07/23/2020, 12:44 PM
Not yet, will try
j

Joost Klitsie

07/23/2020, 12:45 PM
This is highly untested and possibly wrong, but a step in that direction:
Copy code
(ContextAmbient.current as? Activity)?.registerForActivityResult(PostActivityContract()) { result ->
            // parseResult will return this as string?                                              
            if (result != null) toast("Result : $result")
            else toast("No Result")
        }?.launch()
g

galex

07/23/2020, 1:03 PM
Copy code
(ContextAmbient.current as? ComponentActivity)
And it works! Nice 🙂
Now I need to build the Contract for Google Sign-in! Thanks @Joost Klitsie
💯 2
r

Ricardo C.

07/23/2020, 5:31 PM
I would say to be careful with ContextWrappers. You will have to find the activity instead of just casting
j

Joost Klitsie

07/23/2020, 6:53 PM
@Ricardo C. Which context could it be otherwise? With the old views the context was always the activity, do you think it could be something different now?
r

Ricardo C.

07/23/2020, 6:57 PM
It can be a ContextWrapper, I believe
j

Joost Klitsie

07/23/2020, 6:58 PM
Ah yeah. In that case a small search wouldn't be bad :)
3 Views