Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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