Fix calling of binding outside lifecycle (#1169)

This commit is contained in:
Rafał Borcz 2021-03-01 22:57:47 +01:00 committed by GitHub
parent a801c8f8be
commit 8388a8a5fc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,5 +1,7 @@
package io.github.wulkanowy.utils package io.github.wulkanowy.utils
import android.os.Handler
import android.os.Looper
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.lifecycle.Lifecycle 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 private var _value: T? = null
@ -44,11 +47,12 @@ class LifecycleAwareVariableActivity<T : Any> : ReadWriteProperty<AppCompatActiv
@Suppress("unused") @Suppress("unused")
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
fun onDestroyView() { fun onDestroyView() {
_value = null Handler(Looper.getMainLooper()).post {
_value = null
}
} }
} }
@Suppress("unused") @Suppress("unused")
fun <T : Any> Fragment.lifecycleAwareVariable() = LifecycleAwareVariable<T>() fun <T : Any> Fragment.lifecycleAwareVariable() = LifecycleAwareVariable<T>()