forked from github/wulkanowy-mirror
Fix calling of binding outside lifecycle (#1169)
This commit is contained in:
parent
a801c8f8be
commit
8388a8a5fc
@ -1,5 +1,7 @@
|
||||
package io.github.wulkanowy.utils
|
||||
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.lifecycle.Lifecycle
|
||||
@ -28,7 +30,8 @@ class LifecycleAwareVariable<T : Any> : ReadWriteProperty<Fragment, T>, Lifecycl
|
||||
}
|
||||
}
|
||||
|
||||
class LifecycleAwareVariableActivity<T : Any> : ReadWriteProperty<AppCompatActivity, T>, LifecycleObserver {
|
||||
class LifecycleAwareVariableActivity<T : Any> : ReadWriteProperty<AppCompatActivity, T>,
|
||||
LifecycleObserver {
|
||||
|
||||
private var _value: T? = null
|
||||
|
||||
@ -44,11 +47,12 @@ class LifecycleAwareVariableActivity<T : Any> : ReadWriteProperty<AppCompatActiv
|
||||
@Suppress("unused")
|
||||
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
|
||||
fun onDestroyView() {
|
||||
_value = null
|
||||
Handler(Looper.getMainLooper()).post {
|
||||
_value = null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Suppress("unused")
|
||||
fun <T : Any> Fragment.lifecycleAwareVariable() = LifecycleAwareVariable<T>()
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user