0

I have some Activity code that goes like this:

void onCreate(Bundle b) {
    super.onCreate(b);
    setContentView(...);

    EditText et = (EditText) findViewById(...);
    et.setEditableFactory(new Editable.Factory() {
        @Override public Editable newEditable(CharSequence source) {
            return (Editable) source;
        }
    });
    et.setText(new MySpannableStringBuilderSubclass("foo"), TextView.BufferType.EDITABLE);
}

Essentially, I want to have the backing text for an EditText be a custom class I created, MySpannableStringBuilderSubclass. The call to setEditableFactory ensures that setText doesn't copy the contents of my subclass into a SpannableStringBuilder.

Everything above works fine provided the phone doesn't change screen orientation; getText().getClass().getSimpleName() returns MySpannableStringBuilderSubclass as it should. However, if I change the phone's orientation to landscape, the activity is destroyed/re-created. When I call findViewById, the returned EditText already has the text "foo", but getText().getClass().getSimpleName() is SpannableStringBuilder, implying it copied it off my subclass.

How can I prevent this from happening? Thanks.

James Ko
  • 32,215
  • 30
  • 128
  • 239

0 Answers0