-
Notifications
You must be signed in to change notification settings - Fork 112
View.backPressHandler memory leak fix.
#894
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
Conversation
We now strictly require that a `ViewTreeLifecycleOwner` can be found so that we can be certain of getting a callback to remove the `OnBackPressedCallback` from the `OnBackPressedDispatcher`. We also null out the guts of the callback while the `View` is detached, to be doubly sure not to leak anything even if the `ViewTreeLifecycleOwner` is misbehaving. Fixes #889.
90d4cd3 to
f56b3ce
Compare
ef8d100 to
b8ac7ba
Compare
...low-ui/core-android/src/androidTest/java/com/squareup/workflow1/ui/BackPressedHandlerTest.kt
Show resolved
Hide resolved
b8ac7ba to
16de577
Compare
| } | ||
|
|
||
| public ArrayDeque<OnBackPressedCallback> callbacks() { | ||
| return dispatcher.mOnBackPressedCallbacks; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Collection or List maybe?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm exposing the implementation of an Android class for testing purposes, I don't think there's a real need to abstract it.
| lifecycleOrNull?.let { lifecycle -> | ||
| lifecycle.removeObserver(this) | ||
| lifecycleOrNull = null | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit:
lifecycleOrNull?.removeObserver(this)
lifecycleOrNull = null
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In fact you can do
lifecycleOrNull?.removeObserver(this)
lifecycleOrNull = ViewTreeLifecycleOwner.get(view)?.lifecycle
| /** | ||
| * Weak reference ensures that we don't leak the view. | ||
| */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LOL
helios175
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The old code would register with |

We now strictly require that a
ViewTreeLifecycleOwnercan be found so that we can be certain of getting a callback to remove theOnBackPressedCallbackfrom theOnBackPressedDispatcher. We also null out the guts of the callback while theViewis detached, to be doubly sure not to leak anything even if theViewTreeLifecycleOwneris misbehaving.Fixes #889.