-2

I started a TabLayout Activity, which includes the following code to create the fragment:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_find, container, false);
    TextView textView = (TextView) rootView.findViewById(R.id.section_label);
    textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
    return rootView;
}

I've read the official documentation and still unsure how it works. If somebody could explain in detail how each part here is working that would be great.

Edit: Mainly referring to View rootView = inflater.inflate(R.layout.fragment_find, container, false); what each of these 3 parameters are doing and how inflater.inflate() is working here.

Zorgan
  • 8,227
  • 23
  • 106
  • 207
  • 2
    Did you get anything from the docs, or only some parts of this are unclear? If you provide us with any details of what you think it is about, we'll be able to focus on your douts. – gonczor May 04 '17 at 10:41
  • `View rootView = inflater.inflate(R.layout.fragment_find, container, false);` what each of these 3 parameters are doing and how inflater.inflate() is working here. – Zorgan May 04 '17 at 10:44
  • update your question then to make this easily visible for others, please. – gonczor May 04 '17 at 10:59

2 Answers2

0

onCreateView():

After the onCreate() is called (in the Fragment), the Fragment's onCreateView() is called. You can assign your View variables and do any graphical initialisations. You are expected to return a View from this method, and this is the main UI view, but if your Fragment does not use any layouts or graphics, you can return null (happens by default if you don't override).

Here, it's a method of the lifecycle for Fragment. Here, it's a method of lifecycle for Fragment.

HassanUsman
  • 1,787
  • 1
  • 20
  • 38
0

OK, here we go.

The process of inflating is simply creating your view explicitly instead of doing this implicitly, this is by using this:

public void onCreate(){
         setContentView(R.layout.your_layout);
    }

Compare with this question.

Now with the arguments. Compare this with this section.

  • R.layout.fragment_find returns ID of a fragment used somewhere in the code. R is a dynamic android class used for manipulating some of your app's resources such as views, strings etc. Compare.
  • container is a root from some ViewGroup. So you hgave a group of, say, buttons doing common things (for example choosing some colour in your application), and they all have same parent, in your case called a container.
  • attachToRoot is the last argument. According to docs:

    If false, root is only used to create the correct subclass of LayoutParams for the root view in the XML.

So it is not attached to the parent we talked about in last point. Compare here.

I hope this helped.

Community
  • 1
  • 1
gonczor
  • 3,994
  • 1
  • 21
  • 46
  • Thanks this clears things up. Just one thing, I checked and my container is `ViewPager`, is this the same type as `LinearLayout`, `CoordinatorLayout` etc or is it different? – Zorgan May 04 '17 at 21:26
  • Yes, they all inherit from `ViewGroup` – gonczor May 05 '17 at 06:29