When I want to create a simple fragment, usually use following code:
public class TestFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_latout, container, false);
}
}
My questions are:
What exactly is container parameter? where it is defined?
Can we see this ViewGroup(container) in XML/Layout Designer?
In this method(onCreateView), what is this ViewGroup(container) id?
In R.id.x, to access this ViewGroup(container) What should we replace x with?
Thanks.
You don't call that method, the system does, as part of the fragment lifecycle.
When a fragment is added to a layout,
onCreateViewwill eventually get called, andcontaineris the view that holds the fragment. That lets you do things like access the parent's layout parameters, the style/theme applied to it, etc. That's why you passcontainerwhen you callinflate, so it can apply any necessary attributes while inflating the fragment's layout.Basically don't worry about it, and pass the parent/container when you
inflatea layout. 99% of the time, that's all you need to know!