[Reminder]优化View的生命周期感知逻辑
This commit is contained in:
@@ -7,6 +7,7 @@ import android.view.View
|
||||
import android.widget.ListPopupWindow
|
||||
import android.widget.PopupWindow
|
||||
import androidx.annotation.IntRange
|
||||
import androidx.core.view.ViewCompat
|
||||
import androidx.core.view.doOnAttach
|
||||
import androidx.core.view.doOnDetach
|
||||
import androidx.lifecycle.*
|
||||
@@ -111,6 +112,13 @@ interface IReminder : Comparable<IReminder> {
|
||||
private val lifecycle = LifecycleRegistry(this)
|
||||
|
||||
init {
|
||||
if (ViewCompat.isAttachedToWindow(this@lifecycleOwner)) {
|
||||
lifecycle.let {
|
||||
if (it.currentState.isAtLeast(Lifecycle.State.INITIALIZED)) {
|
||||
it.currentState = Lifecycle.State.CREATED
|
||||
}
|
||||
}
|
||||
}
|
||||
addOnAttachStateChangeListener(object : View.OnAttachStateChangeListener {
|
||||
override fun onViewAttachedToWindow(v: View?) {
|
||||
lifecycle.let {
|
||||
@@ -154,7 +162,7 @@ interface IReminder : Comparable<IReminder> {
|
||||
|
||||
override fun getLifecycle(): Lifecycle {
|
||||
val contentView = this@lifecycleOwner.contentView
|
||||
?: throw IllegalStateException("Please ensure ${this@lifecycleOwner.javaClass.simpleName}'s getContentView() can't return null.")
|
||||
?: throw IllegalStateException("please ensure ${this@lifecycleOwner.javaClass.simpleName}'s getContentView() can't return null.")
|
||||
if (!register) {
|
||||
contentView.lifecycleOwner.lifecycle.addObserver(this)
|
||||
register = true
|
||||
@@ -182,7 +190,7 @@ interface IReminder : Comparable<IReminder> {
|
||||
private var register = false
|
||||
|
||||
override fun getLifecycle(): Lifecycle {
|
||||
val contentView = listView ?: throw IllegalStateException("Please ensure ${this@lifecycleOwner.javaClass.simpleName}'s getListView[() can't return null.")
|
||||
val contentView = listView ?: throw IllegalStateException("please ensure ${this@lifecycleOwner.javaClass.simpleName}'s getListView[() can't return null.")
|
||||
if (!register) {
|
||||
contentView.lifecycleOwner.lifecycle.addObserver(this)
|
||||
register = true
|
||||
|
||||
Reference in New Issue
Block a user