11

The following is running on an Android 1.6 so I'm using the compatibility package for fragments. In the following TestFragment is a static nested class:

public class FragmentTestActivity extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}

public static class TestFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        TextView result = new TextView(getActivity());
        result.setText("Hello TestFragment");
        return result;
    }
}

}

The main.xml file:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<fragment class="com.test.FragmentTestActivity$TestFragment"
        android:id="@+id/test"
        android:layout_width="fill_parent" android:layout_height="fill_parent" />
</FrameLayout>

The strange thing is that the container parameter in onCreateView is null.

Now, if I add the fragment programatically like so(just change the onCreate method of the Activity) the container is no longer null. Why?

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Fragment frag = new TestFragment();
    getSupportFragmentManager().beginTransaction().add(android.R.id.content, frag).commit();
}
Roland
  • 7,525
  • 13
  • 61
  • 124
  • 1
    I am having a simmilar issue. I thought the container was part of the activity layout, where the fragment is embedded. How can that be null? – David Miler Sep 21 '12 at 13:41

1 Answers1

2

The documentation mentions that it can be null:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)

[...]

container: If non-null, this is the parent view that the fragment's UI should be attached to. The fragment should not add the view itself, but this can be used to generate the LayoutParams of the view.

To be clear: you shouldn't do anything like container.addView(...).

Timmmm
  • 88,195
  • 71
  • 364
  • 509
  • 6
    That's not what he's doing though. He's saying when inflating from a layout xml, the container is null even though there is a parent view the Fragment is contained in. I don't think that's supposed to happen. – DeeV Nov 27 '12 at 14:58
  • Sorry I wasn't clear. I know he isn't currently doing `container.addView(...)` but I was guessing he wanted to, and that's why he noticed that `container` is sometimes `null`. The docs say "If non-null" which means it is perfectly ok for it to be `null`, and it doesn't matter if it is `null` because you never need to add any views to it. – Timmmm Nov 27 '12 at 16:26
  • I want, for example, to have a BroadcastReceiver which will update the fragment in certain cases. Saving the container for future update would have been a great solution, but having a null container means I must find other way to do it – Shushu Jul 04 '13 at 06:56