fix: back search persist on newer devices #149
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
1. What Was Wrong
The core issue:
OnBackPressedCallbackdoesn't intercept all back presses. The ActionBar/Toolbar has its own back press handling for collapsing expanded ActionViews that runs at a lower level.The back press chain looks like this:
So our
OnBackPressedCallbacknever even got called because the ActionBar intercepted the back press first and collapsed the SearchView.What OS versions/updates caused this?
This has technically always been the case, but it became more noticeable with:
The old approach of overriding
onBackPressed()directly might have worked differently because it was at a different point in the chain. AndroidX'sOnBackPressedCallbackis a higher-level abstraction that doesn't cover ActionBar internals.2. Could We Have Known?
Honestly: not easily. Here's why our tests passed but the device failed:
onBackPressedDispatcher.onBackPressed()goes directly to callbacks.What could have caught this:
OnActionExpandListenerfrom the start - this is the documented way to intercept ActionView collapseThe lesson: For anything involving system UI (navigation, ActionBar, gestures), Robolectric and even emulators can't fully replicate real device behavior. This is why Google recommends testing on multiple real devices with different OS versions.
Note
Implements two-stage SearchView dismissal using the action view’s expand/collapse lifecycle, replacing back-press handling.
OnActionExpandListeneronsearchMenuItemto intercept collapse: ifsearchViewhas focus, clear focus and prevent collapse; otherwise clear active filter and allow collapseOnBackPressedCallbacksearch logic; back behavior falls through to default when no searchsearchMenuItemasinternalfor testsexpandActionView()/collapseActionView()and assert focus/filter behaviorWritten by Cursor Bugbot for commit 63aea76. This will update automatically on new commits. Configure here.