Search code examples
androidandroid-dialogfragmentdialogfragment

DialogFragment crash on second rotation (IllegalStateException)


I am creating a DialogFragment within a fragment hosted by an activity. Here's how I create the dialog:

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

Upon device reconfig (rotate in this case) the dialog is maintained along with the input (inflating a layout with an edittext within the dialogfragment class. However, upon another reconfig, I receive a crash:

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

Solution

  • try this

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