Android why does it NOT throw the wrong thread exception?

I got the impression that the views can only be manipulated from the main thread ... however, why this is NOT a failure:

public class MainActivity extends Activity { TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); tv = new TextView(this); tv.setText("original text"); setContentView(tv); new Thread(new Runnable() { @Override public void run() { tv.setText("trollollolol i should die here but i won't."); } }).start(); } } 

I worked on the device and the emulator, both work fine, and I see the text change. what's happening?

i also checked thread ids, and the background thread is NOT DEFINED, not the main thread (threadID = 1)

+4
source share
1 answer

Android does not actually stop you from updating the user interface outside the main thread. This is an even bigger ticking time bomb. If the main UI thread is not updating the UI at that time, your thread can do this. At least that was my understanding. I do not know, for sure, 100%, but I was able (accidentally) to update the user interface from outside the main user interface. Sometimes it worked, and sometimes it didn’t. But as a good practice, I would use Async task .

+1
source

Source: https://habr.com/ru/post/1213335/


All Articles