Is there an example on how to use the new Activity...
# compose
g
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
Did you try to get the current context, cast it to activity and registerForActivityResult on that one?
g
Not yet, will try
j
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
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
I would say to be careful with ContextWrappers. You will have to find the activity instead of just casting
j
@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
It can be a ContextWrapper, I believe
j
Ah yeah. In that case a small search wouldn't be bad :)