How to get Context in Jetpack Compose (Kotlin)
Hello, colleagues! I've just started learning Jetpack Compose and immediately ran into a problem. In the old XML approach, I was used to getting Context via `this` or `getApplicationContext()`, but in Compose this doesn't work. I need to get Context to, for example, show a `Toast`, access resources (`strings.xml`, colors), or system services like `ConnectivityManager`.
Here's an example of my code that doesn't compile:
```kotlin
@Composable
fun MyScreen() {
// Trying to get Context as usual
val context = this // Error: 'this' is not a Context
// Or like this:
val context = getApplicationContext() // Error: function is not defined
Button(onClick = {
Toast.makeText(context, "Clicked!", Toast.LENGTH_SHORT).show()
}) {
```
No answers yet.
Log in to leave an answer