Skip to content

Commit 43114c4

Browse files
drchenhunterstich
authored andcommitted
[BottomSheet] Fix activity leak when setting states on destroying activities
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
1 parent 37fcd24 commit 43114c4

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

lib/java/com/google/android/material/bottomsheet/BottomSheetBehavior.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1592,7 +1592,7 @@ void startSettlingAnimation(
15921592
settleRunnable = new SettleRunnable(child, state);
15931593
}
15941594
// If the SettleRunnable has not been posted, post it with the correct state.
1595-
if (settleRunnable.isPosted == false) {
1595+
if (!settleRunnable.isPosted) {
15961596
settleRunnable.targetState = state;
15971597
ViewCompat.postOnAnimation(child, settleRunnable);
15981598
settleRunnable.isPosted = true;
@@ -1890,21 +1890,23 @@ public int getLastStableState() {
18901890

18911891
private class SettleRunnable implements Runnable {
18921892

1893-
private final View view;
1893+
private final WeakReference<View> viewRef;
18941894

18951895
private boolean isPosted;
18961896

18971897
@State int targetState;
18981898

18991899
SettleRunnable(View view, @State int targetState) {
1900-
this.view = view;
1900+
this.viewRef = new WeakReference<>(view);
19011901
this.targetState = targetState;
19021902
}
19031903

19041904
@Override
19051905
public void run() {
1906-
if (viewDragHelper != null && viewDragHelper.continueSettling(true)) {
1907-
ViewCompat.postOnAnimation(view, this);
1906+
if (viewRef.get() != null
1907+
&& viewDragHelper != null
1908+
&& viewDragHelper.continueSettling(true)) {
1909+
ViewCompat.postOnAnimation(viewRef.get(), this);
19081910
} else {
19091911
setStateInternal(targetState);
19101912
}

0 commit comments

Comments
 (0)