Fragment.onCreateView ma pusty kontener
Następujący działa na Androidzie 1.6, więc używam pakietu kompatybilności dla fragmentów. W następującymTestFragment
jest statyczną klasą zagnieżdżoną:
<code>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; } } </code>
}
Plik main.xml:
<code><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> </code>
Dziwne jest to, że parametr kontenera wonCreateView
jestnull
.
Teraz, jeśli dodam fragment programowo w ten sposób (po prostu zmieńonCreate
metoda działania) pojemnik nie jest już pusty. Czemu?
<code>public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Fragment frag = new TestFragment(); getSupportFragmentManager().beginTransaction().add(android.R.id.content, frag).commit(); } </code>