What does the LayoutInflater attachToRoot parameter mean?

Solution 1:

NOW OR NOT NOW

The main difference between the "third" parameter attachToRoot being true or false is this.

When you put attachToRoot

true : add the child view to parent RIGHT NOW
false: add the child view to parent NOT NOW.
Add it later. `

When is that later?

That later is when you use for eg parent.addView(childView)

A common misconception is, if attachToRoot parameter is false then the child view will not be added to parent. WRONG
In both cases, child view will be added to parentView. It is just the matter of time.

inflater.inflate(child,parent,false);
parent.addView(child);   

is equivalent to

inflater.inflate(child,parent,true);

A BIG NO-NO
You should never pass attachToRoot as true when you are not responsible for adding the child view to parent.
Eg When adding Fragment

public View onCreateView(LayoutInflater inflater,ViewGroup parent,Bundle bundle)
  {
        super.onCreateView(inflater,parent,bundle);
        View view = inflater.inflate(R.layout.image_fragment,parent,false);
        .....
        return view;
  }

if you pass third parameter as true you will get IllegalStateException because of this guy.

getSupportFragmentManager()
      .beginTransaction()
      .add(parent, childFragment)
      .commit();

Since you have already added the child fragment in onCreateView() by mistake. 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. But I have not tested it though.

Solution 2:

If set to true then when your layout is inflated it will be automatically added to the view hierarchy of the ViewGroup specified in the 2nd parameter as a child. For example if the root parameter was a LinearLayout then your inflated view will be automatically added as a child of that view.

If it is set to false then your layout will be inflated but won't be attached to any other layout (so it won't be drawn, receive touch events etc).

Solution 3:

Seems like a lot of text in the responses but no code, that's why I decided to revive this old question with a code example, in several responses people mentioned:

If set to true then when your layout is inflated it will be automatically added to the view hierarchy of the ViewGroup specified in the 2nd parameter as a child.

What that actually means in code(what most programmers understand) is:

public class MyCustomLayout extends LinearLayout {
    public MyCustomLayout(Context context) {
        super(context);
        // Inflate the view from the layout resource and pass it as child of mine (Notice I'm a LinearLayout class).

        LayoutInflater.from(context).inflate(R.layout.child_view, this, true);
    }
}

Notice that previous code is adding the layout R.layout.child_view as child of MyCustomLayout because of attachToRoot param is true and assigns the layout params of the parent exactly in the same way as if I would be using addView programmatically, or as if I did this in xml:

<LinearLayout>
   <View.../>
   ...
</LinearLayout>

The following code explains the scenario when passing attachRoot as false:

LinearLayout linearLayout = new LinearLayout(context);
linearLayout.setLayoutParams(new LayoutParams(
    LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
linearLayout.setOrientation(LinearLayout.VERTICAL);
    // Create a stand-alone view
View myView = LayoutInflater.from(context)
    .inflate(R.layout.ownRootView, null, false);
linearLayout.addView(myView);

In the previous code you specify that you wanted myView to be it's own root object and do not attach it to any parent, later on we added it as part of the LinearLayout but for a moment it was a stand-alone (no parent) view.

Same thing happens with Fragments, you could add them to an already existing group and be part of it, or just pass the parameters:

inflater.inflate(R.layout.fragment, null, false);

To specify that it will be it's own root.

Solution 4:

The documentation and the two previous answers should be enough, just some thoughts from me.

The inflate method is used to inflate layout files. With those inflated layouts you have to possibility to attach them directly to a parent ViewGroup or just inflate the view hierarchy from that layout file and work with it outside of the normal view hierarchy.

In the first case the attachToRoot parameter will have to be set to true(or much simple use the inflate method that takes a layout file and a parent root ViewGroup(non null)). In this case the View returned is simply the ViewGroup that was passed in the method, the ViewGroup to which the inflated view hierarchy will be added.

For the second option the returned View is the root ViewGroup from the layout file. If you remember our last discussion from the include-merge pair question this is one of the reasons for the merge's limitation(when a layout file with merge as root is inflated, you must supply a parent and attachedToRoot must be set to true). If you had a layout file with the root a merge tag and attachedToRoot was set to false then the inflate method will have nothing to return as merge doesn't have an equivalent. Also, as the documentation says, the inflate version with attachToRoot set to false is important because you can create the view hierarchy with the correct LayoutParams from the parent. This is important in some cases, most notable with the children of AdapterView, a subclass of ViewGroup, for which the addView() methods set is not supported. I'm sure you recall using this line in the getView() method:

convertView = inflater.inflate(R.layout.row_layout, parent, false);

This line ensures that the inflated R.layout.row_layout file has the correct LayoutParams from the AdapterView subclass set on its root ViewGroup. If you wouldn't be doing this you could have some problems with the layout file if the root was a RelativeLayout. The TableLayout/TableRow also have some special and important LayoutParams and you should make sure the views in them have the correct LayoutParams.

Solution 5:

I myself was also confused about what was the real purpose of attachToRoot in inflate method. After a bit of UI study, I finally got the answer:

parent:

in this case is the widget/layout that is surrounding the view objects that you want to inflate using findViewById().

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. if set to false, they are not added as direct children of the parent and the parent doesn't recieve any touch events from the views.

Hope this clears the confusion