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

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.
主站蜘蛛池模板: 田阳县| 安平县| 台北县| 蓝田县| 霞浦县| 榕江县| 琼中| 方正县| 南宁市| 吉木萨尔县| 醴陵市| 合川市| 绥德县| 陆良县| 永州市| 石棉县| 宣恩县| 枣庄市| 玉环县| 天长市| 绥芬河市| 遂昌县| 萝北县| 彰化市| 武清区| 通渭县| 会东县| 上犹县| 西藏| 亳州市| 墨江| 开江县| 福建省| 墨脱县| 灌云县| 民乐县| 宁陵县| 印江| 缙云县| 清新县| 昌平区|