Skip to content

Instantly share code, notes, and snippets.

@samiuelson
samiuelson / LifecycleAwareLazy.kt
Created September 4, 2018 15:34
Property delegate useful for Android development. Allows to declare lazily evaluated properties dependant on Activity Context and avoid memory leaks.
class LifecycleAwareLazy<T>(lifecycle: Lifecycle, private val initializer: () -> T) :
Lazy<T>, GenericLifecycleObserver {
init {
lifecycle.addObserver(this)
}
private object UNINITIALIZED_VALUE
private var _value: Any? = UNINITIALIZED_VALUE