Skip to content

Commit

Permalink
[BottomSheet] Fix activity leak when setting states on destroying act…
Browse files Browse the repository at this point in the history
…ivities

The activity leak can happen in a corner case: setting bottom sheet state when the bottom sheet is dismissed and the host activity is closed. In this situation SettleRunnable will be posted to ViewRootImpl since the bottom sheet is not attached, which leaves references of the dismissed bottom sheet (and thus the destroyed activity) in ViewRootImpl and causes activity leak.

Fixes this by using weak reference in SettleRunnable instead of the strong reference, which is the standard practice of the BottomSheetBehavior class.

Resolves #1417

PiperOrigin-RevId: 430709002
  • Loading branch information
drchen authored and hunterstich committed Feb 25, 2022
1 parent 37fcd24 commit 43114c4
Showing 1 changed file with 7 additions and 5 deletions.
Expand Up @@ -1592,7 +1592,7 @@ void startSettlingAnimation(
settleRunnable = new SettleRunnable(child, state);
}
// If the SettleRunnable has not been posted, post it with the correct state.
if (settleRunnable.isPosted == false) {
if (!settleRunnable.isPosted) {
settleRunnable.targetState = state;
ViewCompat.postOnAnimation(child, settleRunnable);
settleRunnable.isPosted = true;
Expand Down Expand Up @@ -1890,21 +1890,23 @@ public int getLastStableState() {

private class SettleRunnable implements Runnable {

private final View view;
private final WeakReference<View> viewRef;

private boolean isPosted;

@State int targetState;

SettleRunnable(View view, @State int targetState) {
this.view = view;
this.viewRef = new WeakReference<>(view);
this.targetState = targetState;
}

@Override
public void run() {
if (viewDragHelper != null && viewDragHelper.continueSettling(true)) {
ViewCompat.postOnAnimation(view, this);
if (viewRef.get() != null
&& viewDragHelper != null
&& viewDragHelper.continueSettling(true)) {
ViewCompat.postOnAnimation(viewRef.get(), this);
} else {
setStateInternal(targetState);
}
Expand Down

0 comments on commit 43114c4

Please sign in to comment.