How to get Context in Jetpack Compose
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In Jetpack Compose, the standard way to get an Android Context is LocalContext.current. That gives a composable access to the current ambient Android environment so it can perform tasks such as showing a toast, loading resources, or starting an activity.
Use LocalContext.current
The normal pattern is short:
This is the Compose equivalent of accessing context in classic Android UI code.
What Kind of Context You Are Getting
LocalContext.current usually gives you the context associated with the current composition environment. In many cases that is an Activity context, but you should not rely on that unless the code genuinely needs an Activity.
If you only need resource access or a system service, treat it as a Context and keep the dependency narrow.
That matters because not every place in Android should assume it owns an Activity reference.
Starting an Activity
A common use case is launching another screen.
If you are using Compose Navigation, prefer navigation APIs for screen changes inside the app. Use raw Context-based activity launching when that is actually the right tool.
Do Not Store Context Long-Term Without a Reason
A common mistake is capturing LocalContext.current into some long-lived object or view model. In Compose, it is usually better to use the context at the point of need rather than treating it like general application state.
If you need an application-scoped context deliberately, derive it explicitly:
That is safer than accidentally retaining an Activity context longer than intended.
Use the Right Abstraction When Possible
Context is powerful, but it is not always the best dependency to pass around. For example:
- use string resources APIs for UI text
- use Compose navigation instead of manual activity launches when appropriate
- inject platform services at a higher layer when that keeps composables cleaner
Compose works best when the UI layer stays declarative and only dips into Android context where necessary.
Side Effects Still Need Care
Accessing context inside a composable does not mean every platform operation should run during every recomposition. Expensive or one-time work should still be controlled with the appropriate Compose side-effect APIs so that getting the context remains cheap and the actual platform action happens only when intended.
Previews Are a Special Case
Compose previews can also provide a context, but preview behavior is not the same as running inside the full app process. If some context-dependent call behaves strangely only in previews, check whether the code assumes a real activity or runtime environment that the preview does not fully emulate.
Common Pitfalls
- Forgetting that
LocalContext.currentis the standard way to access context in a composable. - Holding on to an
Activitycontext longer than needed. - Using raw
Contextnavigation when Compose Navigation is the cleaner abstraction. - Pulling too many platform concerns directly into composables instead of isolating them.
- Assuming every use of context needs an
Activityrather than a plainContextorapplicationContext.
Summary
- In Jetpack Compose, use
LocalContext.currentto get the current Android context. - This is useful for toasts, intents, resources, and system-service access.
- Prefer the narrowest context usage that solves the problem.
- Avoid storing context in long-lived state unless you intentionally need application scope.
- Use higher-level Compose APIs when they fit better than direct context calls.
Related reading
- How to get Core Data object from specific Object ID?
- How to get current foreground activity context in android?
- How to get current language code with Swift?
- How to get current time and date in Android
- How to get current time and date in Android
- How to get device make and model on iOS?
- How to get enum from raw value in Swift?
- How to get hosting Activity from a view?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.