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

前端 未结 4 689
无人共我
无人共我 2020-12-08 20:09

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

相关标签:
4条回答
  • 2020-12-08 20:09

    NO.

    There is no difference at all.

    0 讨论(0)
  • 2020-12-08 20:14

    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

    0 讨论(0)
  • 2020-12-08 20:25

    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;
    }
    
    0 讨论(0)
  • 2020-12-08 20:30

    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.

    0 讨论(0)
提交回复
热议问题