Thread
#compose
    galex

    galex

    2 years ago
    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

    2 years ago
    Did you try to get the current context, cast it to activity and registerForActivityResult on that one?
    galex

    galex

    2 years ago
    Not yet, will try
    j

    Joost Klitsie

    2 years ago
    This is highly untested and possibly wrong, but a step in that direction:
    (ContextAmbient.current as? Activity)?.registerForActivityResult(PostActivityContract()) { result ->
                // parseResult will return this as string?                                              
                if (result != null) toast("Result : $result")
                else toast("No Result")
            }?.launch()
    galex

    galex

    2 years ago
    (ContextAmbient.current as? ComponentActivity)
    And it works! Nice 🙂
    Now I need to build the Contract for Google Sign-in! Thanks @Joost Klitsie
    Ricardo C.

    Ricardo C.

    2 years ago
    I would say to be careful with ContextWrappers. You will have to find the activity instead of just casting
    j

    Joost Klitsie

    2 years ago
    @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?
    Ricardo C.

    Ricardo C.

    2 years ago
    It can be a ContextWrapper, I believe
    j

    Joost Klitsie

    2 years ago
    Ah yeah. In that case a small search wouldn't be bad 😃