Thread
#compose
    Sivan

    Sivan

    2 years ago
    I am trying to fire an intent to move to the next activity when I click on an ExtendedFloatingActionButton. To obtain the context, I tried
    ContextAmbient.current
    but android studio is unable to generate the error message. So I ended up using the context like this
    val context = ambientOf { ContextAmbient } as Context
    But when I fire the intent, the app crashes with the following error.
    E/AndroidRuntime: FATAL EXCEPTION: main
        Process: com.sivan.pinotes, PID: 25739
        java.lang.ClassCastException: androidx.compose.runtime.DynamicProvidableAmbient cannot be cast to android.content.Context
            at com.sivan.pinotes.MainActivityKt$extendedFAB$2$1.invoke(MainActivity.kt:65)
            at com.sivan.pinotes.MainActivityKt$extendedFAB$2$1.invoke(Unknown Source:0)
            at androidx.compose.foundation.ClickableKt$clickable$2$tap$1.invoke(Clickable.kt:79)
    If DynamicProvidableAmbient cannot be cast to a Context, are there any alternatives to this approach?
    Manuel Wrage

    Manuel Wrage

    2 years ago
    You have to call
    ContextAmbient.current
    Sivan

    Sivan

    2 years ago
    I tried. And it is throwing an unknown error. Running
    1.0.0-alpha01
    btw
    s

    SaurabhS

    2 years ago
    The
    current
    property is a Composable while the onClick lambda isn't. You have to call it outside onClick. Something like
    val context = ContextAmbient.current
    and then use the context in onClick.
    Sivan

    Sivan

    2 years ago
    Ahh. Works. Thanks buddy 🙂