Open VincentJoshuaET opened 3 years ago
@VincentJoshuaET LifecycleObserver
documentation now says "Don't use this interface directly. Instead implement either DefaultLifecycleObserver
or LifecycleEventObserver
to be notified about lifecycle events."
OnLifecycleEvent
is deprecated and gives additional details for this: "This annotation required the usage of code generation or reflection, which should be avoided. Use DefaultLifecycleObserver or LifecycleEventObserver instead."
I submitted this in 2020. Here is the updated code:
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import com.couchbase.lite.Replicator
class ReplicatorLifecycleObserver(private val replicator: Replicator) : DefaultLifecycleObserver {
override fun onStart(owner: LifecycleOwner) {
replicator.start()
}
override fun onStop(owner: LifecycleOwner) {
replicator.stop()
}
}
fun Replicator.bindToLifecycle(lifecycle: Lifecycle) {
lifecycle.addObserver(ReplicatorLifecycleObserver(this))
}
fun Replicator.bindToLifecycle(lifecycleOwner: LifecycleOwner) {
lifecycleOwner.lifecycle.addObserver(ReplicatorLifecycleObserver(this))
}
This doesn't need
Lifecycle.Event.ON_DESTROY -> source.lifecycle.removeObserver(this)
anymore.