I ran into a painful bug recently, and finally managed to procure a minimal repro. Here it is:
private EditText mEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// This check ensures the code below is not run when onCreate() is called
// due to a change in screen orientation.
// See https://stackoverflow.com/a/11811999/4077294
if (savedInstanceState == null) {
// In the layout: main_editText has is an EditText with its text set to "Hello world!"
mEditText = (EditText) findViewById(R.id.main_editText);
workLoop(0);
}
}
private void workLoop(final int index) {
Editable text = mEditText.getText();
if (index == text.length()) {
return;
}
text.setSpan(new ForegroundColorSpan(Color.BLUE), index, index + 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
Handler uiThreadHandler = new Handler(Looper.getMainLooper());
uiThreadHandler.postDelayed(new Runnable() {
@Override
public void run() {
workLoop(index + 1);
}
}, 5000);
}
When you run this code and keep the device in portrait mode, it will color each character of Hello world!
blue in 5 second intervals.
When you run this code and change it to landscape mode, it will abruptly stop coloring characters blue. I determined via the debugger that setSpan()
is still being called, but it doesn't seem to be affecting the color. Why is this?