From e026496e939a05b29c122a7ae1ef0c23d66d1cfb Mon Sep 17 00:00:00 2001 From: skydoves Date: Thu, 10 Jun 2021 00:45:19 +0900 Subject: [PATCH 1/2] Unbind binding property when the lifecycle is on destroyed --- .../main/java/com/skydoves/bindables/BindingActivity.kt | 9 +++++++++ .../bindables/BindingBottomSheetDialogFragment.kt | 1 + .../com/skydoves/bindables/BindingComponentActivity.kt | 9 +++++++++ .../java/com/skydoves/bindables/BindingDialogFragment.kt | 1 + .../main/java/com/skydoves/bindables/BindingFragment.kt | 1 + .../com/skydoves/bindables/BindingFragmentActivity.kt | 9 +++++++++ 6 files changed, 30 insertions(+) diff --git a/bindables/src/main/java/com/skydoves/bindables/BindingActivity.kt b/bindables/src/main/java/com/skydoves/bindables/BindingActivity.kt index 1fff93d..6a3c945 100644 --- a/bindables/src/main/java/com/skydoves/bindables/BindingActivity.kt +++ b/bindables/src/main/java/com/skydoves/bindables/BindingActivity.kt @@ -67,4 +67,13 @@ abstract class BindingActivity constructor( binding.notifyChange() } } + + /** + * Removes binding listeners to expression variables and destroys the [binding] backing property for preventing + * leaking the [ViewDataBinding] that references the Context. + */ + override fun onDestroy() { + super.onDestroy() + binding.unbind() + } } diff --git a/bindables/src/main/java/com/skydoves/bindables/BindingBottomSheetDialogFragment.kt b/bindables/src/main/java/com/skydoves/bindables/BindingBottomSheetDialogFragment.kt index 871f760..a75762a 100644 --- a/bindables/src/main/java/com/skydoves/bindables/BindingBottomSheetDialogFragment.kt +++ b/bindables/src/main/java/com/skydoves/bindables/BindingBottomSheetDialogFragment.kt @@ -86,6 +86,7 @@ abstract class BindingBottomSheetDialogFragment constructor */ override fun onDestroyView() { super.onDestroyView() + _binding?.unbind() _binding = null } } diff --git a/bindables/src/main/java/com/skydoves/bindables/BindingComponentActivity.kt b/bindables/src/main/java/com/skydoves/bindables/BindingComponentActivity.kt index 42a544a..9e42a97 100644 --- a/bindables/src/main/java/com/skydoves/bindables/BindingComponentActivity.kt +++ b/bindables/src/main/java/com/skydoves/bindables/BindingComponentActivity.kt @@ -67,4 +67,13 @@ abstract class BindingComponentActivity constructor( binding.notifyChange() } } + + /** + * Removes binding listeners to expression variables and destroys the [binding] backing property for preventing + * leaking the [ViewDataBinding] that references the Context. + */ + override fun onDestroy() { + super.onDestroy() + binding.unbind() + } } diff --git a/bindables/src/main/java/com/skydoves/bindables/BindingDialogFragment.kt b/bindables/src/main/java/com/skydoves/bindables/BindingDialogFragment.kt index 73e19f7..84e1610 100644 --- a/bindables/src/main/java/com/skydoves/bindables/BindingDialogFragment.kt +++ b/bindables/src/main/java/com/skydoves/bindables/BindingDialogFragment.kt @@ -86,6 +86,7 @@ abstract class BindingDialogFragment constructor( */ override fun onDestroyView() { super.onDestroyView() + _binding?.unbind() _binding = null } } diff --git a/bindables/src/main/java/com/skydoves/bindables/BindingFragment.kt b/bindables/src/main/java/com/skydoves/bindables/BindingFragment.kt index f3c75a4..8c1a155 100644 --- a/bindables/src/main/java/com/skydoves/bindables/BindingFragment.kt +++ b/bindables/src/main/java/com/skydoves/bindables/BindingFragment.kt @@ -86,6 +86,7 @@ abstract class BindingFragment constructor( */ override fun onDestroyView() { super.onDestroyView() + _binding?.unbind() _binding = null } } diff --git a/bindables/src/main/java/com/skydoves/bindables/BindingFragmentActivity.kt b/bindables/src/main/java/com/skydoves/bindables/BindingFragmentActivity.kt index 5d5e6bf..2dde844 100644 --- a/bindables/src/main/java/com/skydoves/bindables/BindingFragmentActivity.kt +++ b/bindables/src/main/java/com/skydoves/bindables/BindingFragmentActivity.kt @@ -67,4 +67,13 @@ abstract class BindingFragmentActivity constructor( binding.notifyChange() } } + + /** + * Removes binding listeners to expression variables and destroys the [binding] backing property for preventing + * leaking the [ViewDataBinding] that references the Context. + */ + override fun onDestroy() { + super.onDestroy() + binding.unbind() + } } From 2a2ad77c49e0669056c93fc96334c09cc30cb3e3 Mon Sep 17 00:00:00 2001 From: skydoves Date: Thu, 10 Jun 2021 00:46:42 +0900 Subject: [PATCH 2/2] Add clearAllProperties on the BinidngModel --- .../main/java/com/skydoves/bindables/BindingModel.kt | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/bindables/src/main/java/com/skydoves/bindables/BindingModel.kt b/bindables/src/main/java/com/skydoves/bindables/BindingModel.kt index 76c7100..74a0bd1 100644 --- a/bindables/src/main/java/com/skydoves/bindables/BindingModel.kt +++ b/bindables/src/main/java/com/skydoves/bindables/BindingModel.kt @@ -112,4 +112,14 @@ abstract class BindingModel : BindingObservable { propertyCallbacks.notifyCallbacks(this, BR._all, null) } } + + /** + * Clears all binding properties from the callback registry. + */ + override fun clearAllProperties() { + synchronized(lock) lock@{ + val propertyCallbacks = propertyCallbacks ?: return@lock + propertyCallbacks.clear() + } + } }