使用Handler Android [英] Use of Handler Android
问题描述
哪种是使用处理程序的更好方法.任何优势.我遇到的所有示例似乎都提供了内联版本.
在类中使用实现Handler.Callback
并实现接口方法.
或
使用内联代码版本
private Handler mHandler = new Handler(){ ....};
常用术语或这些内联类定义是匿名类.
基本上,主要区别在于可读性,编码速度,重用性和范围.
从资源的角度来看,匿名类的创建可能会导致垃圾收集器的开销,如 解决方案
The common term or these inline class definitions is Anonymous Classes.
You can read more about the discussion on these in Java/Android: anonymous local classes vs named classes
Essentially the main differences are readbility, speed of coding, re-use and scope.
From a resource point of view the anonymous class creation may cause an overhead in the garbage collector as discussed in Avoid Creating Unnecessary Objects. I am not certain on the exact details of anonymous class creation, however, it is logical that implementing the interface on the class is more efficient.
@WilliamTMallard has provided an example of what NOT to do. In his example, a long and syntacticly complex handler should be implementented on the class rather than anonymous handler because it is harder to read and edit when defined inline.
这篇关于使用Handler Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!