What does the LayoutInflater attachToRoot parameter mean? What does the LayoutInflater attachToRoot parameter mean? android android

What does the LayoutInflater attachToRoot parameter mean?


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.


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).


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 viewView 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.