I have an Activity that calls setContentView with this XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
>
<fragment android:name="org.vt.indiatab.GroupFragment"
android:id="@+id/home_groups"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" />
<..some other fragments ...>
</LinearLayout>
The GroupFragment extends Fragment, and all is well there. However, I show a DialogFragment from within GroupFragment. This shows correctly, HOWEVER when the screen rotates, I get a Force Close.
What’s the proper way to display a DialogFragment from within another Fragment other than DialogFragment.show(FragmentManager, String)?
OK, while Zsombor’s method works, this is due to me being inexperienced with Fragments and his solution causes issues with the
saveInstanceState Bundle.Apparently (at least for a DialogFragment), it should be a
public static class. You also MUST write your ownstatic DialogFragment newInstance()method. This is because the Fragment class calls thenewInstancemethod in itsinstantiate()method.So in conclusion, you MUST write your DialogFragments like so:
And show them with:
This may be unique to the ActionBarSherlock Library, but the official samples in the SDK documentation use this paradigm also.