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

Sivan

09/01/2020, 7:01 AM
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
Copy code
val context = ambientOf { ContextAmbient } as Context
But when I fire the intent, the app crashes with the following error.
Copy code
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?
m

Manuel Wrage

09/01/2020, 7:23 AM
You have to call
ContextAmbient.current
s

Sivan

09/01/2020, 7:26 AM
I tried. And it is throwing an unknown error. Running
1.0.0-alpha01
btw
s

SaurabhS

09/01/2020, 7:37 AM
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.
🙌 1
2
s

Sivan

09/01/2020, 7:44 AM
Ahh. Works. Thanks buddy 🙂
👍 1
7 Views