Skip to content

Conversation

@williscool
Copy link
Owner

@williscool williscool commented Jan 3, 2026

1. What Was Wrong

The core issue: OnBackPressedCallback doesn'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:

User presses back
       ↓
┌─────────────────────────────────────┐
│  ActionBar/Toolbar checks:          │  ← Happens FIRST
│  "Is there an expanded ActionView?" │
│  If yes → collapse it, DONE         │
└─────────────────────────────────────┘
       ↓ (only if no ActionView)
┌─────────────────────────────────────┐
│  OnBackPressedDispatcher            │  ← Our callback lives here
│  Calls registered callbacks         │
└─────────────────────────────────────┘
       ↓
┌─────────────────────────────────────┐
│  Default behavior (finish/bg)       │
└─────────────────────────────────────┘

So our OnBackPressedCallback never 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:

  • Android 10 (API 29): Gesture navigation became default, back handling got more complex
  • Android 13 (API 33): Predictive back animations introduced, more strict back handling
  • Android 14-16: Further refinements to back gesture system

The old approach of overriding onBackPressed() directly might have worked differently because it was at a different point in the chain. AndroidX's OnBackPressedCallback is 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:

Test Type Why It Passed Reality
Robolectric Simulates Android but doesn't replicate ActionBar back handling. onBackPressedDispatcher.onBackPressed() goes directly to callbacks. Real ActionBar intercepts first
Instrumentation/Espresso Probably ran on emulator with slightly different timing/state, or older Android version Pixel 9 Pro Fold with latest Android 16

What could have caught this:

  1. Testing on real flagship devices with latest OS - exactly what you did
  2. Reading ActionView documentation carefully - it mentions the collapse-on-back behavior but doesn't explicitly say it bypasses OnBackPressedCallback
  3. Using OnActionExpandListener from the start - this is the documented way to intercept ActionView collapse

The 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.

  • Add OnActionExpandListener on searchMenuItem to intercept collapse: if searchView has focus, clear focus and prevent collapse; otherwise clear active filter and allow collapse
  • Remove custom OnBackPressedCallback search logic; back behavior falls through to default when no search
  • Expose searchMenuItem as internal for tests
  • Update Robolectric tests to drive search via expandActionView()/collapseActionView() and assert focus/filter behavior

Written by Cursor Bugbot for commit 63aea76. This will update automatically on new commits. Configure here.

@github-actions
Copy link

github-actions bot commented Jan 3, 2026

Build artifacts for PR #149 (commit d691eef) are available:

You can download these artifacts from the "Artifacts" section of the workflow run.

@github-actions
Copy link

github-actions bot commented Jan 3, 2026

Code Coverage Report

Overall Project 21.36% -0.01%
Files changed 88.17% 🍏

File Coverage
MainActivity.kt 47.91% -0.57% 🍏

@github-actions
Copy link

github-actions bot commented Jan 3, 2026

📊 Code Coverage Summary

Coverage Type Coverage
Overall 21.36
Changed Files 47.91

View detailed coverage report

@williscool williscool merged commit 789f334 into master Jan 3, 2026
16 of 17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants