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

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.
主站蜘蛛池模板: 新乡市| 永安市| 历史| 江源县| 博野县| 沁源县| 西贡区| 闽侯县| 沽源县| 武胜县| 湖口县| 武强县| 潮安县| 北川| 贺州市| 河津市| 泾阳县| 方城县| 皋兰县| 宁海县| 大新县| 石嘴山市| 拜城县| 龙胜| 灯塔市| 万年县| 竹溪县| 石首市| 高唐县| 南澳县| 屯昌县| 米泉市| 广西| 大竹县| 巩义市| 沅江市| 始兴县| 六枝特区| 巴青县| 江川县| 德州市|