question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Hilt] Lint flags generated DialogFragment class with "Error: Use of LayoutInflater.from... detected. Consider using getLayoutInflater() instead"

See original GitHub issue

I 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:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Chang-Ericcommented, Feb 28, 2022

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!

0reactions
alvindizoncommented, Feb 9, 2022

Ah got it, @Chang-Eric thanks 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use getActivity.getLayoutInflater() in custom dialog class
Try below code. View view=LayoutInflater.from(context).inflate(R.layout.dialog_text,null);. Instead of LayoutInflater inflater=getActivity() ...
Read more >
Diff - platform/frameworks/support - Google Git
Merge "Stub out compat classes for Zoom and EvComp" into androidx-master-dev diff --git a/.github/ISSUE_TEMPLATE/config.yml ...
Read more >
Exploring Android, FINAL Edition - CommonsWare
This book has you build an app from the beginning. Whereas traditional programming guides are focused on breadth and depth, this book is...
Read more >
Fragment | Android Developers
Returns the cached LayoutInflater used to inflate Views of this Fragment. ... Instead of using a target fragment to pass results, use setFragmentResult...
Read more >
Fix Hilt Android Gradle plugin is applied but no ... - YouTube
Sometimes, when importing an Hilt Android project, you get this error : Hilt Android Gradle plugin is applied but no ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found