Stefan
Stefan

Reputation: 4221

How can I use LocalContext inside LaunchedEffect?

Is there a way to obtain Context within a LaunchedEffect? I need to call a certain function which needs the reference of Context object in order to execute.

I cannot use LocalContext.current, because:

@Composable invocations can only happen from the context of a @Composable function

Upvotes: 7

Views: 2160

Answers (1)

Phil Dukhov
Phil Dukhov

Reputation: 87605

You can get context value in view builder context and then use it in LaunchedEffect:

val context = LocalContext.current
LaunchedEffect(context) {
    context
}

Upvotes: 16

Related Questions