When should we use android.arch.lifecycle:compiler (or android.arch.lifecycle:common-java8)?
when do we need to use
annotationProcessor "android.arch.lifecycle:compiler:1.1.1"
AFAIK, that's only needed if you have lifecycle-related annotations in your code, specifically @OnLifecycleEvent
.
Or implementation "android.arch.lifecycle:common-java8:1.1.1" since we are using Java 8?
Same thing. The docs state "If your app uses Java 8, we recommend using this library instead of android.arch.lifecycle:compiler
."
Lifecycle annotation processor dependency declaration for Java 8 should be as following:
implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"
Instead of:
kapt "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"
Your options
As far as I can see you have 3 options:
LifecycleObserver
It's a marker interface, it doesn't have any methods. Your class will implement it and then you define a bunch of @OnLifecycleEvent
methods as you need.
Lifecycle runtime will do one of 2 things:
- Use reflection to look up the annotated methods,
- or use generated adapters if you enabled the
lifecycle-compiler
annotation processor.
This interface is part of the lifecycle-common
library.
LifecycleEventObserver
It provides a single method
void onStateChanged(@NonNull LifecycleOwner source, @NonNull Lifecycle.Event event);
which will get called instead of any annotated methods.
This interface is part of the lifecycle-common
library.
DefaultLifecycleObserver
It provides an interface with several empty methods:
default void onCreate(@NonNull LifecycleOwner owner) {}
default void onStart(@NonNull LifecycleOwner owner) {}
default void onResume(@NonNull LifecycleOwner owner) {}
default void onPause(@NonNull LifecycleOwner owner) {}
default void onStop(@NonNull LifecycleOwner owner) {}
default void onDestroy(@NonNull LifecycleOwner owner) {}
Your class will implement this interface and you can pick which methods to implement.
This interface is part of the lifecycle-common-java8
library. Interfaces with some implemented methods (default methods) are supported since Java 8. If your project has enabled Java 8 language features you can use it.
What to make of it
LifecycleEventObserver
and DefaultLifecycleObserver
add methods to your class, this may not be what you want. It's definitely not what I like.
I'd expect that you create a method with semantically accurate name and tell the Lifecycle framework only when it should call it. Like so:
@OnLifecycleEvent(Lifecycle.Event.ON_START)
public void startListening();
It doesn't pollute your class with extra methods. And you can use the annotation processor to make it faster at run-time. (The generated adapter is still looked up using reflection.)
I find this statement from Lifecycle release notes inaccurate:
annotationProcessor "androidx.lifecycle:lifecycle-compiler:$lifecycle_version" // For Kotlin use kapt instead of annotationProcessor // alternately - if using Java8, use the following instead of lifecycle-compiler implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"
The compiler generates an adapter so you don't have to alter your class' interface. It works totally different from DefaultLifecycleObserver
.