Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem when getting context in android Custom View inflated in Fragment #3417

Closed
abrolrahul opened this issue Jun 2, 2022 · 1 comment
Closed

Comments

@abrolrahul
Copy link

Hello,
I have the following configuration using Hilt 2.32-alpha, I set up the annotation @androidentrypoint on a Fragment. This fragment declares a custom view in its layout.

The problem occurs when I was trying to get context (basically activity) from inside the Custom view class and I'm getting FragmentContextWrapper: this class is not intended to be used outside the Hilt components as mentioned in the documentation.

So I'm trying to figure out how to get the context correctly or if I'm doing something wrong in the hilt implementation?
Otherwise, I want to verify if this is a known bug in the hilt implementation.

Thank you for your help.
Rahul

@Chang-Eric
Copy link
Member

In general, you shouldn't assume that a context is exactly a particular object like the Activity. You should only assume that a context eventually wraps the Activity. This is true even without Hilt since Android does have context wrappers for themes for example. To get the activity from the view, you should take the context, check if it is the Activity, and if not, unwrap it by getting its base context, and repeating and until you find the Activity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants