7

I sometimes inject link to LayoutInflater by Dagger, and produce it in module from Application context like this: LayoutInflater.from(application);. It reduces lines of code.

But colleges tell me that it is wrong way, and it has to be given from Activity context by LayoutInflater.from(MainActivity.this);

Is it true? Does behaviour of layout inflater depend on type of context?

tse
  • 5,769
  • 6
  • 38
  • 58

4 Answers4

3

Yes it is true. There's a big difference considering styles.

The LayoutInflater creates the views by calling their constructors. There it passes the context you passed to it. So if you use the application context instead of an activity context you might lack some information.

It's the same issue like using application context for creating views directly. Activities may define different styles and their contexts wrap these information.

Taking into consideration how you could get it, there's not a big difference. Internally LayoutInflater.cloneInContext(Context) is called to apply different context configurations.

Create a copy of the existing LayoutInflater object, with the copy pointing to a different Context than the original. This is used by ContextThemeWrapper to create a new LayoutInflater to go along with the new Context theme.

With the application context you don't get this.

tynn
  • 38,113
  • 8
  • 108
  • 143
2

If I understand things correctly, in case of using application context for creating LayoutInflater you have a chance to loose your Theme settings. See here for more details.

UPDATED

From source code of layout inflater:

Object[] args = mConstructorArgs;
args[1] = attrs;

constructor.setAccessible(true);
final View view = constructor.newInstance(args);
if (view instanceof ViewStub) {
    // Use the same context when inflating ViewStub later.
    final ViewStub viewStub = (ViewStub) view;
    viewStub.setLayoutInflater(cloneInContext((Context) args[0]));
}
return view;

As you can see, your context (in your case application context) passes in constructions of View. It means that scope of your view will be application, not activity.

Community
  • 1
  • 1
Michael Spitsin
  • 2,539
  • 2
  • 19
  • 29
1

If we Application context means, the inflater instance exist throughout the application until the app is killed. In other case, if we use activity context the inflater instance will be removed once activity is destroyed.

Bhagya
  • 114
  • 6
-1

you can use getApplicationContext() when you know you need a Context for something that may live longer than any other likely Context you have at your disposal like services .

So It is best practice to use activity context when you dont need your object that will hold for long time or to global scale.

Hope it will help.

KDeogharkar
  • 10,939
  • 7
  • 51
  • 95
  • 1
    Yes, thank you. But question was about LayoutInflater. Will I look any difference in UI if I use LayoutInflater produced from the Application context? – tse Sep 21 '16 at 07:18
  • No you wont. So use activity context for better memory management. – KDeogharkar Sep 21 '16 at 07:19