Skip to content

Commit

Permalink
[Predictive Back][Search] Add ability to opt-out of back handling
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 522626762
  • Loading branch information
dsn5ft authored and paulfthomas committed Apr 10, 2023
1 parent 203d5ec commit 176ce5e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/java/com/google/android/material/search/SearchView.java
Expand Up @@ -150,6 +150,7 @@ public class SearchView extends FrameLayout implements CoordinatorLayout.Attache
private final SearchViewAnimationHelper searchViewAnimationHelper;
@NonNull private final MaterialBackOrchestrator backOrchestrator =
new MaterialBackOrchestrator(this);
private final boolean backHandlingEnabled;
private final ElevationOverlayProvider elevationOverlayProvider;
private final Set<TransitionListener> transitionListeners = new LinkedHashSet<>();

Expand Down Expand Up @@ -193,6 +194,7 @@ public SearchView(@NonNull Context context, @Nullable AttributeSet attrs, int de
animatedMenuItems = a.getBoolean(R.styleable.SearchView_animateMenuItems, true);
boolean hideNavigationIcon = a.getBoolean(R.styleable.SearchView_hideNavigationIcon, false);
autoShowKeyboard = a.getBoolean(R.styleable.SearchView_autoShowKeyboard, true);
backHandlingEnabled = a.getBoolean(R.styleable.SearchView_backHandlingEnabled, true);

a.recycle();

Expand Down Expand Up @@ -781,8 +783,9 @@ void setTransitionState(@NonNull TransitionState state) {
listener.onStateChanged(this, previousState, state);
}

// Only automatically handle back if we have a search bar to collapse to.
if (searchBar != null) {
// Only automatically handle back if we have a search bar to collapse to, and if back handling
// is enabled for the SearchView.
if (searchBar != null && backHandlingEnabled) {
if (state.equals(TransitionState.SHOWN)) {
backOrchestrator.startListeningForBackCallbacks();
} else if (state.equals(TransitionState.HIDDEN)) {
Expand Down
Expand Up @@ -99,6 +99,9 @@
<attr name="autoShowKeyboard" format="boolean"/>
<!-- Layout resource to inflate as the header. -->
<attr name="headerLayout"/>
<!-- Whether the automatic back handling behavior should be enabled.
Default is true. -->
<attr name="backHandlingEnabled" format="boolean"/>
</declare-styleable>

</resources>

0 comments on commit 176ce5e

Please sign in to comment.