Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between calling LayoutInflater directly and not?

I went through some tutorials, and in the Android Doc, it says not to access LayoutInflater directly when instantiating it. Example from the google Doc:

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

The tutorial I went through is this one:

LayoutInflater inflater = LayoutInflater.from(parent.getContext());

So what I don't really understand is what the difference is besides the obvious different code. Any explanation much appreciated. I assume that the Android Doc should be the one we follow, but I am not sure if it makes a difference.

like image 299
Andy Avatar asked May 27 '12 19:05

Andy


People also ask

What does LayoutInflater in Android do?

LayoutInflater is used to create a new View (or Layout ) object from one of your xml layouts. findViewById just gives you a reference to a view than has already been created.

Why attachToRoot is false?

Calling add will tell you that child view is already added to parent Hence IllegalStateException. Here you are not responsible for adding childView, FragmentManager is responsible. So always pass false in this case. NOTE: I have also read that parentView will not get childView touchEvents if attachToRoot is false.

What is attach to root in Android?

attachToRoot: attaches the views to their parent (includes them in the parent hierarchy), so any touch event that the views recieve will also be transfered to parent view. Now it's upto the parent whether it wants to entertain those events or ignore them.

How do I get LayoutInflater from context?

Use getLayoutInflater() to get the LayoutInflater instead of calling LayoutInflater. from(Context).


2 Answers

If you open up the Android source you can see that the LayoutInflator.from method looks like so:

/**
 * Obtains the LayoutInflater from the given context.
 */
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;
}

That means the two lines of code in your question do the same thing. Not sure what the tutorial you read says exactly but I don't see any difference in functionality. Using the from method is nice since it requires a little less typing, that's it.

like image 167
satur9nine Avatar answered Nov 01 '22 16:11

satur9nine


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

You are getting LayoutInflater Service Provider from System Manager

LayoutInflater inflater = LayoutInflater.from(parent.getContext());

You are using static method from LayoutInflater Class

I would say that difference is only in code and how you write this also calling stack but result is same - you will get LayoutInflater.

More about this

Regards

like image 2
Simon Dorociak Avatar answered Nov 01 '22 15:11

Simon Dorociak