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

Understanding Android event requirements

Android has a number of requirements around events delivered from the user interface that are important to be aware of, because they directly affect the user's experience and the perceived performance of your application. Android runs the main thread of an application as an event loop, rather than having a separate event loop or event dispatcher thread. This is an extremely important concept to understand, because this thread and event queue are shared between the following:

  • All the events from the user interface
  • The drawing requests from the widgets, where they paint themselves
  • The layout system and all the calculations for positioning and sizing widget
  • A variety of system-level events (such as network state changes)

This makes the main thread of the application a precious resource--every frame of an animation has to run through this event loop as a separate event, as does every layout pass, and every event from the user interface widget. On the other side of this contract, there are three other important factors to know and understand:

  • All method calls to user interface elements must be done on the main thread
  • No networking is allowed on the main thread
  • Every event slice on the main thread is externally timed, and long-running events may cause your application to be terminated through an Application Not Responding dialog displayed to the user (which is as bad as crashing in most cases)

So, it's vital that we have models in place to avoid using the main thread more than is absolutely required. Every time you run something on the main thread, you're taking time away from vital systems such as graphics rendering and input events. This will cause your application to appear to stutter and become unresponsive. Fortunately, Android has many utilities to help, and there are a few extra steps that can be taken as a developer, which will reduce the complexity and help ensure the best possible user experience.

主站蜘蛛池模板: 高州市| 二手房| 平远县| 雅安市| 将乐县| 双柏县| 贵溪市| 榕江县| 浦江县| 咸宁市| 沁阳市| 夏河县| 苏州市| 赤壁市| 蓬安县| 共和县| 且末县| 囊谦县| 金阳县| 石泉县| 尖扎县| 赤壁市| 繁峙县| 濮阳县| 安宁市| 祁东县| 和平县| 颍上县| 双辽市| 南开区| 镇安县| 霍林郭勒市| 江北区| 睢宁县| 兴仁县| 奉贤区| 游戏| 平南县| 四子王旗| 阿荣旗| 抚远县|