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?
Every Activity has a Lifecycle. This Activity will be a Lifecycle Owner(any activity or fragment can be called a lifecycle owner). When an activity is initialized LifecycleOwner is the one that will be constructed initially. Any class that implements the LifeCycleOwner interface indicates that it has Android LifeCycle.
When using a ViewModel exposing LiveData s for a Fragment , there are currently two LifecycleOwners that can be used to oberve: the Fragment itself or. the Fragment 's property mViewLifecycleOwner .
ProcessLifecycleOwner. Class that provides lifecycle for the whole application process. A class that has an Android lifecycle. These events can be used by custom components to handle lifecycle changes without implementing any code inside the Activity or the Fragment.
Step 1: Define LiveData object to store data and a method to observe that data in View class. Step 3: Define viewModel object in activity. this refers to an instance of lifecycleOwner . We are observing Data changes in activity so activity will be notified when live data has been set.
They are using reflection to find annotated functions with @OnLifecycleEvent
. This is the real need why classes should implement LifecycleObserver
. If there was kapt
to do, that probably there shouldn't have been any interface to implement.
The resolution is on runtime, since the retention is set to RetentionPolicy.RUNTIME
.
Reflection is expensive and therefore they are building static
cache of each methods and uses the method reference, yes still reflection, to invoke each of them. I have no figures to provide how directly it affects the start up time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With