Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does inflater require Activity's context?

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?

like image 540
tse Avatar asked Sep 21 '16 07:09

tse


People also ask

What is layout Inflater and why do we use the layout Inflater in listview?

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.

What is difference between activity context and Applicationcontext?

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.

What does it mean to inflate a layout?

"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.

What is layout Inflater in Kotlin?

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.


3 Answers

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.

like image 133
tynn Avatar answered Oct 18 '22 02:10

tynn


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.

like image 42
Michael Spitsin Avatar answered Oct 18 '22 02:10

Michael Spitsin


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.

like image 1
Bhagya Avatar answered Oct 18 '22 02:10

Bhagya