Home » Android » android – Is there any difference between getLayoutInflater() and .getSystemService(Context.LAYOUT_INFLATER_SERVICE)

android – Is there any difference between getLayoutInflater() and .getSystemService(Context.LAYOUT_INFLATER_SERVICE)

Posted by: admin April 23, 2020 Leave a comment


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

How to&Answers:


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;


There is at least one situation that only


must be used instead of the counterpart


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.


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



There is no difference at all.