Skip to content

Commit 2c23d2a

Browse files
dsn5ftpekingme
authored andcommitted
[Predictive Back][Bottom Sheet] Fix bug where standard hideable bottom sheets don't stay hidden after predictive back
It wasn't an issue for modal bottom sheets because they dismiss the whole window when the bottom sheet behavior is hidden Also updated Bottom App Bar demo drawer to opt into predictive back, which is a standard hideable bottom sheet PiperOrigin-RevId: 520312905
1 parent 1159923 commit 2c23d2a

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

catalog/java/io/material/catalog/bottomappbar/BottomAppBarMainDemoFragment.java

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import io.material.catalog.R;
2020

21+
import android.os.Build.VERSION_CODES;
2122
import android.os.Bundle;
2223
import androidx.appcompat.app.AppCompatActivity;
2324
import androidx.appcompat.widget.Toolbar;
@@ -28,10 +29,12 @@
2829
import android.view.View;
2930
import android.view.ViewGroup;
3031
import android.view.accessibility.AccessibilityEvent;
32+
import android.window.BackEvent;
3133
import androidx.activity.OnBackPressedCallback;
3234
import androidx.annotation.LayoutRes;
3335
import androidx.annotation.NonNull;
3436
import androidx.annotation.Nullable;
37+
import androidx.annotation.RequiresApi;
3538
import androidx.coordinatorlayout.widget.CoordinatorLayout;
3639
import com.google.android.material.bottomappbar.BottomAppBar;
3740
import com.google.android.material.bottomappbar.BottomAppBarTopEdgeTreatment;
@@ -56,9 +59,27 @@ public class BottomAppBarMainDemoFragment extends DemoFragment {
5659

5760
private final OnBackPressedCallback bottomDrawerOnBackPressedCallback =
5861
new OnBackPressedCallback(/* enabled= */ false) {
62+
@RequiresApi(VERSION_CODES.UPSIDE_DOWN_CAKE)
63+
@Override
64+
public void handleOnBackStarted(@NonNull BackEvent backEvent) {
65+
bottomDrawerBehavior.startBackProgress(backEvent);
66+
}
67+
68+
@RequiresApi(VERSION_CODES.UPSIDE_DOWN_CAKE)
69+
@Override
70+
public void handleOnBackProgressed(@NonNull BackEvent backEvent) {
71+
bottomDrawerBehavior.updateBackProgress(backEvent);
72+
}
73+
5974
@Override
6075
public void handleOnBackPressed() {
61-
bottomDrawerBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
76+
bottomDrawerBehavior.handleBackInvoked();
77+
}
78+
79+
@RequiresApi(VERSION_CODES.UPSIDE_DOWN_CAKE)
80+
@Override
81+
public void handleOnBackCancelled() {
82+
bottomDrawerBehavior.cancelBackProgress();
6283
}
6384
};
6485

@@ -196,9 +217,22 @@ protected void setUpBottomDrawer(View view) {
196217
new BottomSheetCallback() {
197218
@Override
198219
public void onStateChanged(@NonNull View bottomSheet, int newState) {
199-
bottomDrawerOnBackPressedCallback.setEnabled(
200-
newState == BottomSheetBehavior.STATE_EXPANDED
201-
|| newState == BottomSheetBehavior.STATE_HALF_EXPANDED);
220+
switch (newState) {
221+
case BottomSheetBehavior.STATE_EXPANDED:
222+
case BottomSheetBehavior.STATE_HALF_EXPANDED:
223+
bottomDrawerOnBackPressedCallback.setEnabled(true);
224+
break;
225+
case BottomSheetBehavior.STATE_COLLAPSED:
226+
case BottomSheetBehavior.STATE_HIDDEN:
227+
bottomDrawerOnBackPressedCallback.setEnabled(false);
228+
break;
229+
case BottomSheetBehavior.STATE_DRAGGING:
230+
case BottomSheetBehavior.STATE_SETTLING:
231+
default:
232+
// Do nothing, only change callback enabled for "stable" states.
233+
break;
234+
}
235+
202236
if (newState == BottomSheetBehavior.STATE_HIDDEN) {
203237
barNavView.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
204238
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1597,6 +1597,9 @@ public void handleBackInvoked() {
15971597
public void onAnimationEnd(Animator animation) {
15981598
// Hide immediately following the built-in predictive back slide down animation.
15991599
setStateInternal(STATE_HIDDEN);
1600+
if (viewRef != null && viewRef.get() != null) {
1601+
viewRef.get().requestLayout();
1602+
}
16001603
}
16011604
});
16021605
} else {

0 commit comments

Comments
 (0)