Skip to content

Instantly share code, notes, and snippets.

@panpf
Last active December 8, 2023 07:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save panpf/ff8dd7a95153ce9861f4e2547b848d16 to your computer and use it in GitHub Desktop.
Save panpf/ff8dd7a95153ce9861f4e2547b848d16 to your computer and use it in GitHub Desktop.
为 View 提供 Lifecycle 支持
val View.lifecycleOwner: LifecycleOwner
get() {
synchronized(this) {
/*
* After Lifecycle enters the DESTROYED state, its coroutineScope will be canceled and can no longer be used.
* So you must create a new Lifecycle every time onAttachedToWindow
*/
check(ViewCompat.isAttachedToWindow(this)) {
"View.lifecycleOwner can only be called after onAttachedToWindow and before onDetachedFromWindow"
}
val tag = getTag(R.id.tagId_viewLifecycle)
if (tag != null && tag is ViewLifecycleOwner) {
return tag
} else {
val viewLifecycleOwner = ViewLifecycleOwner(this)
setTag(R.id.tagId_viewLifecycle, viewLifecycleOwner)
return viewLifecycleOwner
}
}
}
class ViewLifecycleOwner(view: View) : LifecycleOwner {
private val lifecycleRegistry = LifecycleRegistry(this)
override val lifecycle: Lifecycle
get() = lifecycleRegistry
init {
lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE)
lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START)
lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_RESUME)
view.addOnAttachStateChangeListener(object : View.OnAttachStateChangeListener {
override fun onViewAttachedToWindow(v: View) {
}
override fun onViewDetachedFromWindow(v: View) {
lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_PAUSE)
lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_STOP)
lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY)
/*
* After Lifecycle enters the DESTROYED state, its coroutineScope will be canceled and can no longer be used.
* So you must create a new Lifecycle every time onAttachedToWindow
*/
view.setTag(R.id.tagId_viewLifecycle, null)
view.removeOnAttachStateChangeListener(this)
}
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment