官术网_书友最值得收藏!

What happens when the UI is blocked

It's a good opportunity to take a practical approach in order to further understand why the UI thread of an Android application should not be blocked. Let's add this simple block of code to MainActivity, and run the application again:

override fun onResume() {
super.onResume()
Thread.sleep(5000)
}

This will block the UI thread for five seconds. If you run the application again, you will notice how the screen is completely white during those five seconds. This would be dreadful for the user of the application.

Not only should the UI thread never be blocked, it should also not perform tasks that are CPU-intensive because this would result in a similar experience for the user. In short, you should only use the UI thread to create and update views, anything in between should be done in a background thread.
主站蜘蛛池模板: 界首市| 那曲县| 乌兰察布市| 定结县| 文昌市| 宜章县| 扎鲁特旗| 新民市| 沙坪坝区| 舟山市| 福海县| 东至县| 赤城县| 营山县| 万荣县| 肇州县| 淄博市| 双流县| 开化县| 永定县| 胶南市| 珲春市| 大悟县| 广东省| 那曲县| 松阳县| 印江| 建湖县| 青河县| 新沂市| 安新县| 靖江市| 招远市| 南开区| 新田县| 无棣县| 海南省| 彩票| 射洪县| 周口市| 昭觉县|