Skip to content

Commit

Permalink
[Predictive Back][Bottom Sheet] Fix bug where standard hideable botto…
Browse files Browse the repository at this point in the history
…m 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
  • Loading branch information
dsn5ft authored and pekingme committed Mar 29, 2023
1 parent 1159923 commit 2c23d2a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
Expand Up @@ -18,6 +18,7 @@

import io.material.catalog.R;

import android.os.Build.VERSION_CODES;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
Expand All @@ -28,10 +29,12 @@
import android.view.View;
import android.view.ViewGroup;
import android.view.accessibility.AccessibilityEvent;
import android.window.BackEvent;
import androidx.activity.OnBackPressedCallback;
import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import com.google.android.material.bottomappbar.BottomAppBar;
import com.google.android.material.bottomappbar.BottomAppBarTopEdgeTreatment;
Expand All @@ -56,9 +59,27 @@ public class BottomAppBarMainDemoFragment extends DemoFragment {

private final OnBackPressedCallback bottomDrawerOnBackPressedCallback =
new OnBackPressedCallback(/* enabled= */ false) {
@RequiresApi(VERSION_CODES.UPSIDE_DOWN_CAKE)
@Override
public void handleOnBackStarted(@NonNull BackEvent backEvent) {
bottomDrawerBehavior.startBackProgress(backEvent);
}

@RequiresApi(VERSION_CODES.UPSIDE_DOWN_CAKE)
@Override
public void handleOnBackProgressed(@NonNull BackEvent backEvent) {
bottomDrawerBehavior.updateBackProgress(backEvent);
}

@Override
public void handleOnBackPressed() {
bottomDrawerBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
bottomDrawerBehavior.handleBackInvoked();
}

@RequiresApi(VERSION_CODES.UPSIDE_DOWN_CAKE)
@Override
public void handleOnBackCancelled() {
bottomDrawerBehavior.cancelBackProgress();
}
};

Expand Down Expand Up @@ -196,9 +217,22 @@ protected void setUpBottomDrawer(View view) {
new BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
bottomDrawerOnBackPressedCallback.setEnabled(
newState == BottomSheetBehavior.STATE_EXPANDED
|| newState == BottomSheetBehavior.STATE_HALF_EXPANDED);
switch (newState) {
case BottomSheetBehavior.STATE_EXPANDED:
case BottomSheetBehavior.STATE_HALF_EXPANDED:
bottomDrawerOnBackPressedCallback.setEnabled(true);
break;
case BottomSheetBehavior.STATE_COLLAPSED:
case BottomSheetBehavior.STATE_HIDDEN:
bottomDrawerOnBackPressedCallback.setEnabled(false);
break;
case BottomSheetBehavior.STATE_DRAGGING:
case BottomSheetBehavior.STATE_SETTLING:
default:
// Do nothing, only change callback enabled for "stable" states.
break;
}

if (newState == BottomSheetBehavior.STATE_HIDDEN) {
barNavView.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
}
Expand Down
Expand Up @@ -1597,6 +1597,9 @@ public void handleBackInvoked() {
public void onAnimationEnd(Animator animation) {
// Hide immediately following the built-in predictive back slide down animation.
setStateInternal(STATE_HIDDEN);
if (viewRef != null && viewRef.get() != null) {
viewRef.get().requestLayout();
}
}
});
} else {
Expand Down

0 comments on commit 2c23d2a

Please sign in to comment.