Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unbind binding property when the lifecycle is on destroyed #12

Merged
merged 2 commits into from
Jun 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
}
}
}