フラグメント、ダイアログフラグメント、画面の回転 質問する

フラグメント、ダイアログフラグメント、画面の回転 質問する

次の XML を使用して setContentView を呼び出す Activity があります。

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

GroupFragment は Fragment を拡張しており、すべて順調です。ただし、GroupFragment 内から DialogFragment を表示します。これは正しく表示されますが、画面が回転すると、強制終了が発生します。

DialogFragment.show(FragmentManager, String) 以外で、別の Fragment 内から DialogFragment を表示する適切な方法は何ですか?

ベストアンサー1

互換性ライブラリにバグがあり、これが原因となっている可能性があります。ダイアログフラグメントに以下を追加してみてください:

@Override
public void onDestroyView() {
  if (getDialog() != null && getRetainInstance())
    getDialog().setOnDismissListener(null);
  super.onDestroyView();
}

また、回転後にダイアログフラグメントが閉じられないように、ダイアログフラグメントを保持として設定することをお勧めします。たとえば、onCreate() メソッドに「setRetainInstance(true);」と入力します。

おすすめ記事