I am trying to fire an intent to move to the next ...
# compose
s
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
You have to call
ContextAmbient.current
s
I tried. And it is throwing an unknown error. Running
1.0.0-alpha01
btw
s
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
Ahh. Works. Thanks buddy 🙂
👍 1