user5645695
user5645695

Reputation:

Inflater in Android Fragments

View rootView = inflater.inflate(R.layout.check_fragment,container,false);

What is the use of a bool as in this case false ? I am a beginner to Android Programming , can someone explain this to me in detail ? Thanks in advance.

Upvotes: 4

Views: 105

Answers (4)

aelimill
aelimill

Reputation: 1015

https://developer.android.com/reference/android/view/LayoutInflater.html

If you set it as true, then the view will be automatically added to its parent (second param). It most of times it should be false, but sometimes it is needed especially when you're using <merge> as root in inflated xml.

Upvotes: 1

marcinj
marcinj

Reputation: 49986

From docs:

attachToRoot - Whether the inflated hierarchy should be attached to the root parameter? If false, root is only used to create the correct subclass of LayoutParams for the root view in the XML.

In fragments you should pass false as an attach argument, this way the view hierarchy will not be attached to the ViewGroup parent passed in the onCreateView. This attachment will happen later on, Android will take care of it. The container is only passed to onCreateView so you can know about the container where your fragments view hierarchy is going to go.

Actually setting this parameter to true will likely cause exceptions or at least some strange behaviour.

Upvotes: 0

Emran
Emran

Reputation: 1031

Whether the inflated hierarchy should be attached to the root parameter? If false, root is only used to create the correct subclass of LayoutParams for the root view in the XML.

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1006674

true means "please add the inflated View to container as a child for me". false means "please do not add the inflated View to container as a child for me, as other code will handle that later on".

In the case of fragments, you allow the FragmentManager to control adding and removing the fragment's View from its container.

The reason why you need container at all in inflate() is because certain layout manager classes (notably RelativeLayout) need to know their container in order to set up their layout rules properly.

Upvotes: 0

Related Questions