How to initialize or access packageManager out from coroutineContext?
Asked Answered
B

2

7

I have a Jetpack Compose project were I can access a coroutineContext object only. No context available here.

How can I access or initialize android.content.pm.PackageManager ?

Bolivia answered 15/10, 2020 at 18:15 Comment(0)
M
4

You can get the context object from ContextAmbient.current, using that you can get the PackageManager

Example:

val context = ContextAmbient.current
val packageManager = context.packageManager
Mneme answered 15/10, 2020 at 18:37 Comment(1)
Getting context now is val context = LocalContext.currentSulfide
S
2

To get context in Jetpack Compose:

val context = LocalContext.current

And to get packageManager from the context:

val packageManager = context.packageManager

So, if you just want packageManager you can use this:

val packageManager = LocalContext.current.packageManager

And for your information, LocalContext.current is a composable and need to be called in a composable context.

Sandisandidge answered 24/12, 2023 at 16:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.