Skip to content

Fix arrivals onBackPressed() on Android 13+#1563

Merged
aaronbrethorst merged 1 commit into
OneBusAway:mainfrom
robertgodfrey:fix-arrivals-back-button
May 28, 2026
Merged

Fix arrivals onBackPressed() on Android 13+#1563
aaronbrethorst merged 1 commit into
OneBusAway:mainfrom
robertgodfrey:fix-arrivals-back-button

Conversation

@robertgodfrey

@robertgodfrey robertgodfrey commented May 16, 2026

Copy link
Copy Markdown
Contributor

Problem

Pressing back while viewing a stop's arrivals exited the app instead of dismissing the panel and returning to the map.

Root cause

HomeActivity overrode onBackPressed() to handle panel dismissal, but this override was silently bypassed on Android 13+ (API 33+). Modern Android routes back presses through WindowOnBackInvokedDispatcherAndroidX OnBackPressedDispatcher, whose fallback calls ComponentActivity.super.onBackPressed() directly — skipping subclass overrides entirely. The app has used androidx.activity:activity-ktx:1.9.3 for about 2 years, which made this the behavior on all currently-supported devices.

Fix

Migrate the back press logic from onBackPressed() into an OnBackPressedCallback registered with getOnBackPressedDispatcher() in onCreate(). This is the API that both the AndroidX dispatcher and the OS-level OnBackInvokedDispatcher actually invoke, and it works correctly on all API levels (not just 13+).

Behavior

  • Panel expanded/anchored → collapse to header
  • Panel collapsed → dismiss arrivals, return to map
  • No panel showing → exit as before

Before:

before.mp4

After:

after.mp4
  • Apply the AndroidStyle.xml style template to your code in Android Studio.

  • Run the unit tests with gradlew connectedObaGoogleDebugAndroidTest to make sure you didn't break anything

  • If you have multiple commits please combine them into one commit by squashing them for the initial submission of the pull request. When addressing comments on a pull request, please push a new commit per comment when possible (reviewers will squash and merge using GitHub merge tool)

Summary by CodeRabbit

  • Chores
    • Updated back button navigation handling to use modern Android framework patterns.

Review Change Stack

@coderabbitai

coderabbitai Bot commented May 16, 2026

Copy link
Copy Markdown

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 1be213d6-965b-4fc6-8194-46f12be5d246

📥 Commits

Reviewing files that changed from the base of the PR and between e65872a and 47b34a8.

📒 Files selected for processing (1)
  • onebusaway-android/src/main/java/org/onebusaway/android/ui/HomeActivity.java

📝 Walkthrough

Walkthrough

HomeActivity migrates system back press handling from the deprecated onBackPressed() override to the modern OnBackPressedDispatcher callback pattern. The sliding panel collapse and map focus clear logic is preserved in the callback registered during onCreate().

Changes

Back Press Handling Migration

Layer / File(s) Summary
Migrate to OnBackPressedDispatcher
onebusaway-android/src/main/java/org/onebusaway/android/ui/HomeActivity.java
Import OnBackPressedCallback, register a callback in onCreate() to intercept back presses and collapse the sliding panel when expanded/anchored or clear map focus when collapsed, then remove the deprecated onBackPressed() override. Behavior remains identical.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely summarizes the main change: fixing the back button behavior on Android 13+ by moving from onBackPressed() override to OnBackPressedDispatcher callback.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Tip

💬 Introducing Slack Agent: The best way for teams to turn conversations into code.

Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.

  • Generate code and open pull requests
  • Plan features and break down work
  • Investigate incidents and troubleshoot customer tickets together
  • Automate recurring tasks and respond to alerts with triggers
  • Summarize progress and report instantly

Built for teams:

  • Shared memory across your entire org—no repeating context
  • Per-thread sandboxes to safely plan and execute work
  • Governance built-in—scoped access, auditability, and budget controls

One agent for your entire SDLC. Right inside Slack.

👉 Get started


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@aaronbrethorst aaronbrethorst left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rob, this is a really nicely scoped fix. The root-cause writeup in the PR description is genuinely illuminating — pinning the silent bypass on WindowOnBackInvokedDispatcherOnBackPressedDispatcherComponentActivity.super.onBackPressed() skipping subclass overrides is exactly the kind of explanation that makes a one-file diff easy to land with confidence. The Before/After videos are also a great touch.

I cross-checked the migration against the official Android docs (Provide custom back navigation; Add support for the predictive back gesture) and the implementation matches the documented pattern step-for-step.

Critical Issues (0 found)

None.

Important Issues (0 found)

None.

Suggestions (1 found)

  • The fallback inside handleOnBackPressed() uses the documented setEnabled(false); getOnBackPressedDispatcher().onBackPressed(); pattern, which permanently disables the callback after first fallback [HomeActivity.java:428-429]. In practice this is fine for HomeActivity because the only time the fallback runs is when no panel is showing and the activity is about to finish, so the disabled state never matters. Worth being aware of as a subtle behavior of this idiom, but no change needed here.

Follow-up (out of scope, tracked separately)

The same Android 13+ bug almost certainly affects two other activities still using the deprecated onBackPressed() override:

  • TripPlanActivity.java:261-267 — has panel-collapse logic structurally identical to the bug fixed here.
  • InfrastructureIssueActivity.java:722-726 — its post-back spinner reset is being silently skipped on API 33+.

I've opened #1566 to track these migrations so they don't get lost.

thanks for knocking this out!

@aaronbrethorst aaronbrethorst merged commit 555cfd5 into OneBusAway:main May 28, 2026
3 checks passed
@robertgodfrey robertgodfrey deleted the fix-arrivals-back-button branch May 28, 2026 23:06
aaronbrethorst pushed a commit that referenced this pull request Jun 13, 2026
…sedCallback

On Android 13+ (API 33+), back presses route through OnBackPressedDispatcher,
whose fallback calls ComponentActivity.super.onBackPressed() directly,
silently bypassing subclass overrides of onBackPressed().

Migrate the two remaining affected activities to register an
OnBackPressedCallback in onCreate(), mirroring the HomeActivity fix from #1563:

- TripPlanActivity: panel-collapse logic now runs on back press again.
  Also route the toolbar home button through the dispatcher so software
  and hardware back stay consistent.
- InfrastructureIssueActivity: the services spinner reset now runs after
  each back press again. The callback temporarily disables itself to let
  the fragment back stack pop (or the activity finish), then re-enables
  for subsequent presses.
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