Skip to content

Commit

Permalink
fix: 修复了声明周期问题,以及销毁之后再次使用,由于 delegate property 会被再次初始化,出现的异常 (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-dhl committed Apr 27, 2022
1 parent e0a2488 commit fa65b08
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ dependencies {
ext.material = "1.2.1"
ext.appcompat = "1.2.0"
ext.ktx = "1.3.2"
ext.remote = true
ext.remote = false

implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation "androidx.core:core-ktx:${ktx}"
Expand Down
25 changes: 14 additions & 11 deletions binding/src/main/java/com/hi/dhl/binding/base/FragmentDelegate.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.hi.dhl.binding.base

import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.viewbinding.ViewBinding
import com.hi.dhl.binding.observerWhenCreated
import com.hi.dhl.binding.observerWhenDestroyed
import kotlin.properties.ReadOnlyProperty

/**
Expand All @@ -21,18 +21,21 @@ abstract class FragmentDelegate<T : ViewBinding>(

init {

/**
* 感谢 architecture-components-samples 提供的思路
*
* 最原始的处理的方案 监听 Fragment 的生命周期,会存在 Fragment 和 Fragment 中的 View 生命周期不一致问题
* 详情查看 [issue][https://github.com/hi-dhl/Binding/issues/15]
*/
// 详情查看 [issue][https://github.com/hi-dhl/Binding/issues/31#issuecomment-1109729949]
fragment.lifecycle.observerWhenCreated {
fragment.viewLifecycleOwnerLiveData.observe(fragment) { viewOwner ->
viewOwner.lifecycle.observerWhenDestroyed {
destroyed()
val fragmentManager = fragment.requireFragmentManager()
fragmentManager.registerFragmentLifecycleCallbacks(object :
FragmentManager.FragmentLifecycleCallbacks() {
override fun onFragmentViewDestroyed(fm: FragmentManager, f: Fragment) {
super.onFragmentViewDestroyed(fm, f)
// 检查 fragment 的目的,为了防止类似于加载多个 Fragment 场景销毁的时候,出现不必要的异常
if (f == fragment) {
destroyed()
fragmentManager.unregisterFragmentLifecycleCallbacks(this)
}

}
}
}, false)
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.hi.dhl.binding.viewbind

import androidx.fragment.app.Fragment
import androidx.lifecycle.Lifecycle
import androidx.viewbinding.ViewBinding
import com.hi.dhl.binding.base.FragmentDelegate
import com.hi.dhl.binding.bindMethod
import com.hi.dhl.binding.inflateMethod
import java.lang.Exception
import kotlin.reflect.KProperty

/**
Expand All @@ -28,6 +30,21 @@ class FragmentViewBinding<T : ViewBinding>(

} ?: let {

try {
/**
* 检查目的,是为了防止在 onCreateView() or after onDestroyView() 使用 binding。
* 另外在销毁之后,如果再次使用,由于 delegate property 会被再次初始化
*
* 捕获这个异常的原因,是为了兼容之前的版本,防止因为升级,造成崩溃,如果不正确使用,将会输出一些日志作为警告
*/
check(thisRef.viewLifecycleOwner.lifecycle.currentState.isAtLeast(Lifecycle.State.INITIALIZED)) {
"cannot use binding in before onCreateView() or after onDestroyView() from 1.1.3. about [issue](https://github.com/hi-dhl/Binding/issues/31#issuecomment-1109733307)"
}
} catch (e: Exception) {
e.printStackTrace()
}


val bind: T
if (thisRef.view == null) {
// 这里为了兼容在 navigation 中使用 Fragment
Expand Down

0 comments on commit fa65b08

Please sign in to comment.