fix(frame): 修复 ViewBinding 内存泄漏的问题,参照官方写法
This commit is contained in:
@ -19,11 +19,15 @@ import com.quyunshuo.androidbaseframemvvm.base.utils.status.imp.BaseFrameViewSta
|
|||||||
* @author Qu Yunshuo
|
* @author Qu Yunshuo
|
||||||
* @since 8/27/20
|
* @since 8/27/20
|
||||||
*/
|
*/
|
||||||
abstract class BaseFrameFragment<VB : ViewBinding, VM : BaseViewModel> : BaseFrameStatusFragment(), FrameView<VB> {
|
abstract class BaseFrameFragment<VB : ViewBinding, VM : BaseViewModel> : BaseFrameStatusFragment(),
|
||||||
|
FrameView<VB> {
|
||||||
|
|
||||||
protected val mBinding: VB by lazy(mode = LazyThreadSafetyMode.NONE) {
|
/**
|
||||||
BindingReflex.reflexViewBinding(javaClass, layoutInflater)
|
* 私有的 ViewBinding 此写法来自 Google Android 官方
|
||||||
}
|
*/
|
||||||
|
private var _binding: VB? = null
|
||||||
|
|
||||||
|
protected val mBinding get() = _binding!!
|
||||||
|
|
||||||
protected abstract val mViewModel: VM
|
protected abstract val mViewModel: VM
|
||||||
|
|
||||||
@ -32,13 +36,13 @@ abstract class BaseFrameFragment<VB : ViewBinding, VM : BaseViewModel> : BaseFra
|
|||||||
*/
|
*/
|
||||||
private lateinit var mBaseStatusHelper: BaseFrameViewStatusHelperImp
|
private lateinit var mBaseStatusHelper: BaseFrameViewStatusHelperImp
|
||||||
|
|
||||||
|
|
||||||
override fun onCreateView(
|
override fun onCreateView(
|
||||||
inflater: LayoutInflater,
|
inflater: LayoutInflater,
|
||||||
container: ViewGroup?,
|
container: ViewGroup?,
|
||||||
savedInstanceState: Bundle?
|
savedInstanceState: Bundle?
|
||||||
): View? {
|
): View? {
|
||||||
return mBinding.root
|
_binding = BindingReflex.reflexViewBinding(javaClass, layoutInflater)
|
||||||
|
return _binding?.root
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||||
@ -48,7 +52,7 @@ abstract class BaseFrameFragment<VB : ViewBinding, VM : BaseViewModel> : BaseFra
|
|||||||
// 注册EventBus
|
// 注册EventBus
|
||||||
if (javaClass.isAnnotationPresent(EventBusRegister::class.java)) EventBusUtils.register(this)
|
if (javaClass.isAnnotationPresent(EventBusRegister::class.java)) EventBusUtils.register(this)
|
||||||
|
|
||||||
mBinding.initView()
|
_binding?.initView()
|
||||||
initObserve()
|
initObserve()
|
||||||
initRequestData()
|
initRequestData()
|
||||||
}
|
}
|
||||||
@ -60,6 +64,11 @@ abstract class BaseFrameFragment<VB : ViewBinding, VM : BaseViewModel> : BaseFra
|
|||||||
return mBaseStatusHelper
|
return mBaseStatusHelper
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun onDestroyView() {
|
||||||
|
super.onDestroyView()
|
||||||
|
_binding = null
|
||||||
|
}
|
||||||
|
|
||||||
override fun onDestroy() {
|
override fun onDestroy() {
|
||||||
if (javaClass.isAnnotationPresent(EventBusRegister::class.java)) EventBusUtils.unRegister(
|
if (javaClass.isAnnotationPresent(EventBusRegister::class.java)) EventBusUtils.unRegister(
|
||||||
this
|
this
|
||||||
|
|||||||
@ -37,5 +37,4 @@ open class BaseFrameStatusActivity : AppCompatActivity() {
|
|||||||
protected open fun onRegisterStatusHelper(): ViewStatusHelper? {
|
protected open fun onRegisterStatusHelper(): ViewStatusHelper? {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -38,5 +38,4 @@ open class BaseFrameStatusFragment : Fragment() {
|
|||||||
protected open fun onRegisterStatusHelper(): ViewStatusHelper? {
|
protected open fun onRegisterStatusHelper(): ViewStatusHelper? {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user