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 thenewInstance
method 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.
To overcome the
Bundle
always being null, I save it to a static field inonSaveInstanceState
. It's a code smell, but the only solution I found for both restoring the dialog and saving the state.The Bundle reference should be nulled in
onDestroy
.I had a similar issue, however none of the above worked for me. In the end I needed to create the fragment in code instead of in the XML layout.
See: Replacing fragments and orientation change
In
onCreate()
callsetRetainInstance(true)
and then include this:When you call
setRetainInstance(true)
in onCreate(), onCreate() will no longer be called across orientation changes, but onCreateView() will still be called.So you can still save the state to your bundle in
onSaveInstanceState()
and then retrieve it inonCreateView()
:I ran into this on my project and none of the above solutions helped.
If the exception looks something like
It's caused by an issue with a fallback container Id that gets used after rotation. See this ticket for more details:
https://code.google.com/p/android/issues/detail?id=18529
Basically you can prevent the crash by making sure all of your xml fragments have a tag defined in the layout. This prevents the fallback condition from occurring if you rotate when a fragment is visible.
In my case I was able to apply this fix without having to override onDestroyView() or setRetainInstance(true), which is the common recommendation for this situation.
I solved this issue with answers of @ZsomborErdődy-Nagy and @AndyDennie . You must subclass this class and in you parent fragment call
setRetainInstance(true)
, anddialogFragment.show(getFragmentManager(), "Dialog");