android - How @OnLifecycleEvent annotated methods from Architecture Components get hooked up with the LifecycleOwner?
问题描述
It looks like we don't need to use kapt
for @OnLifecycleEvent
annotations to work. So, how do they get hooked up? Is it some kind of runtime annotation processing?
I'm asking because I'm curious what are the costs of using these annotations. Is using them affects application startup time? Or project compile time?
解决方案
他们使用反射来查找带注释的函数@OnLifecycleEvent
。这是类应该实现的真正需要LifecycleObserver
。如果kapt
要做的话,那可能不应该有任何接口要实现。
解决方案是在运行时,因为保留设置为 RetentionPolicy.RUNTIME
。
反射是昂贵的,因此他们正在构建static
每个方法的缓存并使用方法引用,是的,仍然是反射,来调用它们中的每一个。我没有数据可以提供它对启动时间的直接影响。
推荐阅读
- javascript - localstorage 在我的 Angular 应用程序中不起作用
- python - Python:无法写入以附加模式打开的文件
- python - 如何从我的数据框中的特定列中删除 \n
- php - 使用 php 将谷歌分析数据传输到 mysqli 数据库
- python-3.x - conda 的 activate.bat 给出“系统找不到指定的路径”
- c++ - 通过类成员访问运算符访问作用域枚举器
- react-native - 即使延迟后反应本机更新setState也不会发生
- javascript - 变量不会改变值javascript
- aframe - 如何关闭 A 帧相机遮挡剔除?
- .net - .NET 中的 Parallel.ForEach 和有条件的正常循环