[ACCEPTED]-Is there any difference between getLayoutInflater() and .getSystemService(Context.LAYOUT_INFLATER_SERVICE)-android-widget

Accepted answer
Score: 43


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

Proof You can trace 3 the LayoutInflater returned by getLayoutInflater() to LayoutInflater.from() and 2 you can see this is just a shortcut for 1 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;
Score: 4

There is at least one situation that only 10


must 9 be used instead of the counterpart


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

Score: 3

This is how you define a LayoutInflater.

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

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

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

So, actually 2 there is NO difference between the two.

Source 1 : Documentation

Score: 2


There is no difference at all.


More Related questions