@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTextView = (TextView) findViewById(R.id.text);
new Thread(new Runnable() {
@Override
public void run() {
mTextView.setText("Hello");
}
}).start();
But if I add delay for the thread, it doesn’t work. So I can manipulate UI elements from a non-UI thread. Can anybody clarify this situation for me?
Android doesn’t actually stop you from updating the UI from outside the main thread. It’s just more of a ticking time bomb. If the main UI thread isn’t updating the UI at the time, then your thread can do it. There’s just no way of controlling when it’s going to happen outside of a
SurfaceView.At least that was my understanding. I don’t 100% know for sure, but I’ve been able to (accidentally) update the UI from outside the main UI thread before. Sometimes it would work and sometimes it wouldn’t, so I assume it was a ConcurrentModificationException of some form.