DialogFragment 在第二次旋转时崩溃(IllegalStateException)

DialogFragment crash on second rotation (IllegalStateException)

我正在 activity 托管的片段中创建 DialogFragment。以下是我创建对话框的方式:

NewDialogEditText ageDialog = new NewDialogEditText.Builder()
            .setCharLimit(2)
            .setInputType(TYPE_CLASS_NUMBER)
            .setTitle(R.string.dialog_title_age)
            .createDialog();
    ageDialog.setTargetFragment(WeightPlanFragment.this, REQUEST_AGE);
    ageDialog.show(getFragmentManager(), DIALOG_AGE);

在设备重新配置(在本例中为旋转)时,对话框与输入一起保持(在对话框片段 class 中使用编辑文本扩充布局)。但是,在另一次重新配置时,我收到崩溃消息:

Failure saving state: NewDialogEditText{5c12148 #0 dialog_age} has target not in fragment manager

试试这个

<activity
            android:name=".MainActivity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar" />