Currently, we are using LiveData, ViewModel and Room in our project.
We are using Java 8.
We use the following in build.gradle
// ViewModel and LiveData
implementation "android.arch.lifecycle:extensions:1.1.1"
// Room (use 1.1.0-beta1 for latest beta)
implementation "android.arch.persistence.room:runtime:1.0.0"
annotationProcessor "android.arch.persistence.room:compiler:1.0.0"
I was wondering, when do we need to use
annotationProcessor "android.arch.lifecycle:compiler:1.1.1"
(Or implementation "android.arch.lifecycle:common-java8:1.1.1" since we are using Java 8?!)
Currently, our code works fine, without using lifecycle:compiler or 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"
As far as I can see you have 3 options:
LifecycleObserverIt'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:
lifecycle-compiler annotation processor.This interface is part of the lifecycle-common library.
LifecycleEventObserverIt 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.
DefaultLifecycleObserverIt 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.
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.
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