Skip to content

Commit 176ce5e

Browse files
dsn5ftpaulfthomas
authored andcommitted
[Predictive Back][Search] Add ability to opt-out of back handling
PiperOrigin-RevId: 522626762
1 parent 203d5ec commit 176ce5e

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

lib/java/com/google/android/material/search/SearchView.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ public class SearchView extends FrameLayout implements CoordinatorLayout.Attache
150150
private final SearchViewAnimationHelper searchViewAnimationHelper;
151151
@NonNull private final MaterialBackOrchestrator backOrchestrator =
152152
new MaterialBackOrchestrator(this);
153+
private final boolean backHandlingEnabled;
153154
private final ElevationOverlayProvider elevationOverlayProvider;
154155
private final Set<TransitionListener> transitionListeners = new LinkedHashSet<>();
155156

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

197199
a.recycle();
198200

@@ -781,8 +783,9 @@ void setTransitionState(@NonNull TransitionState state) {
781783
listener.onStateChanged(this, previousState, state);
782784
}
783785

784-
// Only automatically handle back if we have a search bar to collapse to.
785-
if (searchBar != null) {
786+
// Only automatically handle back if we have a search bar to collapse to, and if back handling
787+
// is enabled for the SearchView.
788+
if (searchBar != null && backHandlingEnabled) {
786789
if (state.equals(TransitionState.SHOWN)) {
787790
backOrchestrator.startListeningForBackCallbacks();
788791
} else if (state.equals(TransitionState.HIDDEN)) {

lib/java/com/google/android/material/search/res/values/attrs.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@
9999
<attr name="autoShowKeyboard" format="boolean"/>
100100
<!-- Layout resource to inflate as the header. -->
101101
<attr name="headerLayout"/>
102+
<!-- Whether the automatic back handling behavior should be enabled.
103+
Default is true. -->
104+
<attr name="backHandlingEnabled" format="boolean"/>
102105
</declare-styleable>
103106

104107
</resources>

0 commit comments

Comments
 (0)