Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Android Fragments Introducing Fragments Adding Fragments to Activities

Why provide attachToRoot as false instead of providing root as null while inflating the fragment?

Hi, in the video Ben Deitch describes providing attachToRoot as false while inflating the fragment as unintuitive - and it is. So why provide a root parameter at all when the aim is to not attach the inflated view to it? (to avoid adding it two times) Why not pass it as null like this? Is there any tradeoff here?

View view = inflater.inflate(R.layout.fragment_list, null); 

1 Answer

Ben Deitch
STAFF
Ben Deitch
Treehouse Teacher

Hey Devanshu! It's just to make sure we're using the correct LayoutParams. Here's what the docs say about it:

ViewGroup: Optional view to be the parent of the generated hierarchy (if attachToRoot is true), or else simply an object that provides a set of LayoutParams values for root of the returned hierarchy (if attachToRoot is false.)

https://developer.android.com/reference/android/view/LayoutInflater.html#inflate(int, android.view.ViewGroup, boolean)