1

I am trying to understand the following fragment code:

public class FragmentA extends Fragment { 
    public FragmentA(){
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
                  if(container!=null)
                      // WHY IS THIS CODE EXECUTED? I did not set container variable


          // why if I pass null insted of Container, I get the same result?
        return inflater.inflate(R.layout.fragment_a,container,false);
    }
}

This ViewGroup container I never initialized or something. How does android know what is my container?, if I never initialized it.

The other thing that is not clear to me is when I call inflate and if instead the container I write null, the result is same.

return inflater.inflate(R.layout.fragment_a,null,false);

I am working with ViewPager in my main activity.

I create the fragment is this way in the FragmentPagerAdapter:

Fragment fragment = new FragmentA();

These are my xml files:

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"/>

and for the fragment :

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFCC00" >

    <!-- TODO: Update blank fragment layout -->

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center"
        android:gravity="center"
          android:textColor="#ffffff"
        android:textStyle="bold"
        android:text="THIS IS FRAGMENT A" />

</FrameLayout>

EDIT: I found this on the developer site:

The container parameter passed to onCreateView() is the parent ViewGroup (from the activity's layout) in which your fragment layout will be inserted. The savedInstanceState parameter is a Bundle that provides data about the previous instance of the fragment, if the fragment is being resumed (restoring state is discussed more in the section about Handling the Fragment Lifecycle).

But, is still not clear for me. I did not pass any ViewGroup to the Fragment in the Fragment call....

user155293
  • 373
  • 1
  • 3
  • 14
  • possible duplicate http://stackoverflow.com/questions/5026926/why-does-layoutinflater-ignore-the-layout-width-and-layout-height-layout-paramet and http://stackoverflow.com/questions/12567578/what-does-the-layoutinflater-attachtoroot-parameter-mean – aldok Mar 29 '16 at 01:37
  • @aldok I edited the question a Little bit. I read your links, but I still do not understand, I dont find my answer – user155293 Mar 29 '16 at 07:23

1 Answers1

0

The container is supplied externally to the fragment. It's either generated automatically when you use the <fragment> tag in xml, or it's passed as part of the FragmentTransaction when you initialize the fragment in code.

RussHWolf
  • 3,555
  • 1
  • 19
  • 26