开发者

Does Android enforce performing UI actions only from the UI thread?

开发者 https://www.devze.com 2023-02-12 19:46 出处:网络
I understand开发者_如何学编程 that Android applications have a single UI thread. Does the runtime enforce that all UI calls are made from this thread, or is it up to the programmer to make sure no UI

I understand开发者_如何学编程 that Android applications have a single UI thread.

Does the runtime enforce that all UI calls are made from this thread, or is it up to the programmer to make sure no UI calls are made from other threads?


If you try to touch a view from another thread other than the owning UI thread you will get a nice CalledFromWrongThreadException.

JAL


You cannot operate on UI items when not on UI thread. For example, you cannot change the text of a textView from a non-UI thread. You are responsible to make sure you are on UI thread when working with UI items.


You need to do every operation with UI widgets in the UI thread. To do so you can use different approaches. I will list a few articles for you to read:

  • Android's Handler
  • Handling Expensive Operations in the UI Thread
  • Painless Threading
0

精彩评论

暂无评论...
验证码 换一张
取 消