32

Simple "No" answer will calm me. If there is any difference then what it is?

Lord_JABA
  • 2,545
  • 7
  • 31
  • 58

4 Answers4

43

No

As long as the Activity or Window that calls getLayoutInflater() has the same Context that would call getSystemService(), there is no difference.


Proof You can trace the LayoutInflater returned by getLayoutInflater() to LayoutInflater.from() and you can see this is just a shortcut for getSystemService() from the source code:

public static LayoutInflater from(Context context) {
    LayoutInflater LayoutInflater =
            (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (LayoutInflater == null) {
        throw new AssertionError("LayoutInflater not found.");
    }
    return LayoutInflater;
}
Sam
  • 86,580
  • 20
  • 181
  • 179
  • 24
    Other proof: ````getLayoutInflater() == getSystemService(Context.LAYOUT_INFLATER_SERVICE)```` returns ````true```` – Brais Gabin Aug 26 '12 at 18:48
  • No, ````==```` returns ````true```` because the two instances are the same. If ````==```` returns ````true```` then ````equals()````, if it's (well) implemented, always will return ````true````. – Brais Gabin Aug 26 '12 at 20:09
4

There is at least one situation that only

getSystemService(Context.LAYOUT_INFLATER_SERVICE);

must be used instead of the counterpart

getLayoutInflater

That situation is in an arbitrary object class. For example, I have an instance of class call objectA. In objectA, I want to inflate a view onto the parent view (happen in ArrayAdapter that inflates customized row on the its listview.) In this case, context.getLayoutInflater does not work since there is no activity or windows associated with the context. Only getSystemService(Context.LAYOUT_INFLATER_SERVICE) is appropriate then.

Driedshrimp
  • 121
  • 1
  • 3
3

This is how you define a LayoutInflater.

LayoutInflater inflater = (LayoutInflater)context.getSystemService (Context.LAYOUT_INFLATER_SERVICE);

And getLayoutInflater() just gives "quick access to the LayoutInflater instance that the window retrieved from its Context" (from the documentation) by returning the LayoutInflater.

Similarly, getSystemService(Context.LAYOUT_INFLATER_SERVICE) is used to retrieve a LayoutInflater for inflating layout resources in this context.

So, actually there is NO difference between the two.

Source : Documentation

Swayam
  • 16,294
  • 14
  • 64
  • 102
  • ["Quick access to the LayoutInflater instance that this Window retrieved from its Context."](http://developer.android.com/reference/android/view/Window.html#getLayoutInflater%28%29) Please don't plagiarize the documentation. Use SO's quote format `>` and cite the source. – Sam Aug 26 '12 at 17:48
2

NO.

There is no difference at all.

Harshil Pansare
  • 1,117
  • 1
  • 13
  • 37