Skip to content

Commit

Permalink
Merge pull request #12 from skydoves/updates/unbind
Browse files Browse the repository at this point in the history
Unbind binding property when the lifecycle is on destroyed
  • Loading branch information
skydoves authored Jun 9, 2021
2 parents efabdf8 + 2a2ad77 commit dd7638f
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,13 @@ abstract class BindingActivity<T : ViewDataBinding> 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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ abstract class BindingBottomSheetDialogFragment<T : ViewDataBinding> constructor
*/
override fun onDestroyView() {
super.onDestroyView()
_binding?.unbind()
_binding = null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,13 @@ abstract class BindingComponentActivity<T : ViewDataBinding> 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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ abstract class BindingDialogFragment<T : ViewDataBinding> constructor(
*/
override fun onDestroyView() {
super.onDestroyView()
_binding?.unbind()
_binding = null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ abstract class BindingFragment<T : ViewDataBinding> constructor(
*/
override fun onDestroyView() {
super.onDestroyView()
_binding?.unbind()
_binding = null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,13 @@ abstract class BindingFragmentActivity<T : ViewDataBinding> 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()
}
}
10 changes: 10 additions & 0 deletions bindables/src/main/java/com/skydoves/bindables/BindingModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
}

0 comments on commit dd7638f

Please sign in to comment.