Mohammad
Mohammad

Reputation: 42

What exactly is container parameter in onCreateView method in Fragment

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.

Upvotes: 0

Views: 571

Answers (1)

cactustictacs
cactustictacs

Reputation: 19592

You don't call that method, the system does, as part of the fragment lifecycle.

When a fragment is added to a layout, onCreateView will eventually get called, and container is 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 pass container when you call inflate, 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 inflate a layout. 99% of the time, that's all you need to know!

Upvotes: 0

Related Questions