首页 > 解决方案 > 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?

标签: androidandroid-architecture-components

解决方案


他们使用反射来查找带​​注释的函数@OnLifecycleEvent。这是类应该实现的真正需要LifecycleObserver。如果kapt要做的话,那可能不应该有任何接口要实现。

解决方案是在运行时,因为保留设置为 RetentionPolicy.RUNTIME

反射是昂贵的,因此他们正在构建static每个方法的缓存并使用方法引用,是的,仍然是反射,来调用它们中的每一个。我没有数据可以提供它对启动时间的直接影响。


推荐阅读