-
Notifications
You must be signed in to change notification settings - Fork 100
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
BackPressHandler leaks the view after its detached #889
Comments
The proposed fix would break the order of the handler in the dispatcher's queue, non-starter. I think the real issue is in the start method. We probably should no-op if we find no fun start() {
view.context.onBackPressedDispatcherOwnerOrNull()
?.let { owner ->
owner.onBackPressedDispatcher.addCallback(owner, onBackPressedCallback)
view.addOnAttachStateChangeListener(this)
if (view.isAttachedToWindow) onViewAttachedToWindow(view)
// We enable the handler only while its view is attached to a window.
// This ensures that a temporarily removed view (e.g. for caching)
// does not participate in back button handling.
ViewTreeLifecycleOwner.get(view)?.lifecycle?.addObserver(this)
}
} |
Problem was introduced at #632. I think the fix is to stop relying on |
Oh my, here's a very naive thing: fun start() {
view.context.onBackPressedDispatcherOwnerOrNull()
?.let { owner ->
owner.onBackPressedDispatcher.addCallback(owner, onBackPressedCallback) We're making the The argument should instead be the |
We cannot. |
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.
HandleBackPressWhenAttached adds a OnBackPressedCallback callback to the OnBackPressedDispatcherOwner implemented by the context of the view, i.e. in this case the activity.
The leak trace indicates that the OnBackPressedCallback is still held by the activity even though the view itself is detached.
workflow-kotlin/workflow-ui/core-android/src/main/java/com/squareup/workflow1/ui/BackPressHandler.kt
Line 89 in 8c524db
We probably shouldn't keep the callback around (and its reference to the view) after the view has been detached.
Leaktrace
https://app.bugsnag.com/square-inc/register-android-dev-leaks/errors/6262b5b5473e980008b4bce8
The text was updated successfully, but these errors were encountered: