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

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.
主站蜘蛛池模板: 龙陵县| 三明市| 肇庆市| 离岛区| 怀安县| 环江| 尼木县| 漳浦县| 灵川县| 突泉县| 汉阴县| 定陶县| 浮山县| 仁怀市| 松阳县| 得荣县| 新余市| 隆子县| 分宜县| 宜宾市| 涞水县| 崇州市| 邵东县| 陇南市| 广平县| 金乡县| 徐水县| 南皮县| 会昌县| 凤台县| 安徽省| 阜阳市| 鱼台县| 远安县| 华亭县| 昌宁县| 江永县| 宣汉县| 华蓥市| 内丘县| 工布江达县|