BlackZerg

Untitled

Oct 19th, 2021
825
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. fun <T> LifecycleOwner.onDestroyNullable(): ReadWriteProperty<LifecycleOwner, T> =
  2.     object : ReadWriteProperty<LifecycleOwner, T>, DefaultLifecycleObserver {
  3.  
  4.         private var value: T? = null
  5.  
  6.         init {
  7.             this@onDestroyNullable
  8.                 .lifecycle
  9.                 .addObserver(this)
  10.         }
  11.  
  12.         override fun onDestroy(owner: LifecycleOwner) {
  13.             value = null
  14.             this@onDestroyNullable
  15.                 .lifecycle
  16.                 .removeObserver(this)
  17.             super.onDestroy(owner)
  18.         }
  19.  
  20.         override fun setValue(thisRef: LifecycleOwner, property: KProperty<*>, value: T) {
  21.             this.value = value
  22.         }
  23.  
  24.         override fun getValue(thisRef: LifecycleOwner, property: KProperty<*>): T {
  25.             return value!!
  26.         }
  27.     }
RAW Paste Data