How to get a context in a recycler view adapter
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Getting a Context inside a RecyclerView.Adapter is a common Android task because adapters often need access to resources, layout inflation, or click handling that starts another screen. The key is not only how to get a context, but also how to do it without creating memory leaks or overly coupling the adapter to an activity.
The Simplest Option: Pass Context In
The most direct pattern is to pass a Context into the adapter constructor.
This works well when the adapter genuinely needs a context for inflation or resource lookup.
Often You Already Have a Better Context
Inside adapter methods, you can frequently use the parent or item view instead of storing a separate field.
This is often preferable because parent.context is available exactly where you need it. It also reduces the chance of holding an unnecessary reference.
Likewise, from a view holder or click listener:
For many use cases, itemView.context is enough.
Pick the Right Kind of Context
Android has multiple context flavors:
- Activity context
- Application context
- Context obtained from a view
If you are inflating themed layouts or launching an activity, an activity-related context is usually the right choice. If you only need something application-wide, such as a simple system service, the application context may be safer.
For example:
Be careful, though. Starting UI flows and applying theme-dependent resources with the wrong context can behave incorrectly.
Avoid Making the Adapter Too Powerful
Adapters are easier to maintain when they focus on binding data rather than owning navigation logic. Instead of letting the adapter directly start activities everywhere, consider passing a click callback from the fragment or activity.
Then the fragment handles the navigation:
This pattern keeps the adapter simpler and avoids overusing Context.
Common Pitfalls
One common mistake is storing an activity context in a long-lived adapter or singleton. If the activity is destroyed, that reference can leak memory.
Another mistake is passing applicationContext everywhere. It works for some operations, but not all UI actions. Layout theming and activity launches can require an activity context.
A third mistake is forgetting that parent.context and itemView.context already exist. Many adapters do not need a dedicated context field at all.
Summary
- You can get a context in a
RecyclerView.Adapterby constructor injection,parent.context, oritemView.context. - '
parent.contextis usually the cleanest option for inflating views.' - Use an activity-related context for UI work and application context only when appropriate.
- Prefer click callbacks to keep navigation logic outside the adapter when possible.
- Be careful not to leak an activity by storing a context longer than necessary.
Related reading
- How to get a Fragment to remove itself, i.e. its equivalent of finish?
- How to Get a Layout Inflater Given a Context?
- How to get a list of installed android applications and pick one to run
- How to get a unique device ID in Swift?
- How to get a unique device ID in Swift?
- How to get Activity's content view?
- How to get AppBar height in Flutter
- How to get async call to return response to main thread, using okhttp?
.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.