[Hilt] Lint flags generated DialogFragment class with "Error: Use of LayoutInflater.from... detected. Consider using getLayoutInflater() instead"
See original GitHub issueI have a class that extends from DialogFragment that is annotated with @AndroidEntryPoint
, something like this:
@AndroidEntryPoint
class OurFragment : DialogFragment() {
I upgraded my project’s Android Gradle Plugin to 7.2.0-alpha06
and Gradle to 7.3.3
, and running lintRelease
produces this error:
Error: Use of LayoutInflater.from(FragmentComponentManager.createContextWrapper(inflater, this)) detected. Consider using getLayoutInflater() instead [UseGetLayoutInflater from fragment-1.4.1]
return LayoutInflater.from(FragmentComponentManager.createContextWrapper(inflater, this));
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Explanation for issues of type "UseGetLayoutInflater":
Using LayoutInflater.from(Context) can return a LayoutInflater
that does not have the correct theme.
Vendor: Android Open Source Project (fragment-1.4.1)
Identifier: fragment-1.4.1
Feedback: https://issuetracker.google.com/issues/new?component=192731
Checking the generated class, I can see this:
/**
* A generated base class to be extended by the @dagger.hilt.android.AndroidEntryPoint annotated class. If using the Gradle plugin, this is swapped as the base class via bytecode transformation.
*/
public abstract class Hilt_OurCardFragment extends DialogFragment implements GeneratedComponentManagerHolder {
...
@Override
public LayoutInflater onGetLayoutInflater(Bundle savedInstanceState) {
LayoutInflater inflater = super.onGetLayoutInflater(savedInstanceState);
return LayoutInflater.from(FragmentComponentManager.createContextWrapper(inflater, this));
}
I’m using version 2.40.5
of Dagger btw. I also have other classes that extend from Fragment
, ~but they do not get flagged by lint, even if their generated classes also use LayoutInflater.from(FragmentComponentManager.createContextWrapper(inflater, this))
~. Now I’m not sure this is the correct place to bring this up, feel free to close this if this is not appropriate, but I’d like to bring this to your attention as I’m not sure how to handle this. I’m thinking of temporarily silencing this error via adding this issue to lint.xml
.
edit: apologies, other classes extending from Fragment also trigger this lint error. I have silenced this error by adding the UseGetLayoutInflater
issue in our lint.xml
.
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (3 by maintainers)
Top GitHub Comments
This will be fixed when that is change is released. It ended up that instead of suppressing the warning, we should have been using cloneInContext(). Thanks for bringing this to our attention!
Ah got it, @Chang-Eric thanks 😃