1

I have a custom DialogPreference with a custom dialog layout. The UI crashes on the second time I click on it, with java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

I tried inflate with attachToRoot equals to false. But then the dialog displayed with empty body.

I am guessing I should call this removeView() when the dialog is dismissed. But I didn't find where should I add that piece of code.

Custom layout:

public class PeriodPicker extends LinearLayout {

    public PeriodPicker(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        inflate(context, R.layout.period_picker, this);    
    }

Custom DialogPreference:

public class PeriodPreference extends DialogPreference {
    public PeriodPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        periodPicker = new PeriodPicker(context, attrs);
    }

    @Override
    protected View onCreateDialogView() {
        return periodPicker;
    }
Sah
  • 1,017
  • 2
  • 11
  • 19

0 Answers0