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?
What is an Inflater ? To summarize what the LayoutInflater Documentation says... A LayoutInflater is one of the Android System Services that is responsible for taking your XML files that define a layout, and converting them into View objects. The OS then uses these view objects to draw the screen.
Application Context: It is the application and we are present in Application. For example - MyApplication(which extends Application class). It is an instance of MyApplication only. Activity Context: It is the activity and we are present in Activity.
"Inflating" a view means taking the layout XML and parsing it to create the view and viewgroup objects from the elements and their attributes specified within, and then adding the hierarchy of those views and viewgroups to the parent ViewGroup.
LayoutInflater. The LayoutInflater instantiates a layout XML file into its corresponding View objects. So with the LayoutInflater object, developers can include an XML View object in another XML layout programmatically. We also can add or update the included View's widgets properties programmatically.
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 differentContext
than the original. This is used byContextThemeWrapper
to create a newLayoutInflater
to go along with the newContext
theme.
With the application context you don't get this.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With