Fragments, DialogFragment, and Screen Rotation

android, android-fragments

Solution

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 own `static DialogFragment newInstance()` method. This is because the Fragment class calls the `newInstance` method in its `instantiate()` method.

So in conclusion, you MUST write your DialogFragments like so:

public static class MyDialogFragment extends DialogFragment {

    static MyDialogFragment newInstance() {
        MyDialogFragment d = new MyDialogFragment();
        return d;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        ...
    }
}

And show them with:

private void showMyDialog() {
    MyDialogFragment d = MyDialogFragment.newInstance();
    d.show(getFragmentManager(), "dialog");
}

This may be unique to the ActionBarSherlock Library, but the official samples in the SDK documentation use this paradigm also.

Problem

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)?

Original source

Related problems