Skip to content

Commit

Permalink
[Predictive Back][Side Sheet] Added predictive back support for modal…
Browse files Browse the repository at this point in the history
… side sheets.

PiperOrigin-RevId: 527334991
  • Loading branch information
afohrman authored and leticiarossi committed Apr 26, 2023
1 parent 562285e commit f335a50
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions lib/java/com/google/android/material/sidesheet/SheetDialog.java
Expand Up @@ -43,6 +43,7 @@
import androidx.core.view.GravityCompat;
import androidx.core.view.ViewCompat;
import androidx.core.view.accessibility.AccessibilityNodeInfoCompat;
import com.google.android.material.motion.MaterialBackOrchestrator;
import com.google.android.material.sidesheet.Sheet.StableSheetState;

/**
Expand All @@ -64,6 +65,8 @@ abstract class SheetDialog<C extends SheetCallback> extends AppCompatDialog {
private boolean canceledOnTouchOutside = true;
private boolean canceledOnTouchOutsideSet;

@Nullable private MaterialBackOrchestrator backOrchestrator;

SheetDialog(
@NonNull Context context,
@StyleRes int theme,
Expand Down Expand Up @@ -117,6 +120,20 @@ public void setCancelable(boolean cancelable) {
if (this.cancelable != cancelable) {
this.cancelable = cancelable;
}
if (getWindow() != null) {
updateListeningForBackCallbacks();
}
}

private void updateListeningForBackCallbacks() {
if (backOrchestrator == null) {
return;
}
if (cancelable) {
backOrchestrator.startListeningForBackCallbacks();
} else {
backOrchestrator.stopListeningForBackCallbacks();
}
}

@Override
Expand All @@ -131,6 +148,15 @@ protected void onStart() {
public void onAttachedToWindow() {
super.onAttachedToWindow();
maybeUpdateWindowAnimationsBasedOnLayoutDirection();
updateListeningForBackCallbacks();
}

@Override
public void onDetachedFromWindow() {
super.onDetachedFromWindow();
if (backOrchestrator != null) {
backOrchestrator.stopListeningForBackCallbacks();
}
}

/**
Expand Down Expand Up @@ -190,6 +216,7 @@ private void ensureContainerAndBehavior() {
sheet = container.findViewById(getDialogId());
behavior = getBehaviorFromSheet(sheet);
addSheetCancelOnHideCallback(behavior);
backOrchestrator = new MaterialBackOrchestrator(behavior, sheet);
}
}

Expand Down

0 comments on commit f335a50

Please sign in to comment.