feat(android): emit cancelable BackButtonPressed event before default navigation - #5891
feat(android): emit cancelable BackButtonPressed event before default navigation#5891mortenolsrud wants to merge 1 commit into
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (6)
🚧 Files skipped from review as they are similar to previous changes (6)
WalkthroughAndroid back-button actions now pass through Go event handling. A consumed event stops further processing. An unconsumed event follows the existing WebView history or activity-exit behavior. ChangesAndroid back-button handling
Estimated code review effort: 3 (Moderate) | ~20 minutes Sequence Diagram(s)sequenceDiagram
participant MainActivity
participant WailsBridge
participant nativeOnBackPressed
participant GoApplication
participant WebView
MainActivity->>WailsBridge: onBackPressed()
WailsBridge->>nativeOnBackPressed: forward back-button action
nativeOnBackPressed->>GoApplication: dispatch BackButtonPressed
GoApplication-->>nativeOnBackPressed: return cancellation state
nativeOnBackPressed-->>WailsBridge: return JNI boolean
WailsBridge-->>MainActivity: return consumed state
alt Event consumed
MainActivity->>MainActivity: stop back-button processing
else Event not consumed
MainActivity->>WebView: navigate back or exit activity
end
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 golangci-lint (2.12.2)level=error msg="[linters_context] typechecking error: pattern ./...: directory prefix . does not contain main module or its selected dependencies" 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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@v3/pkg/application/application_android.go`:
- Around line 746-752: Update the back-button handling around
handleApplicationEvent so registered Go listeners complete before
event.IsCancelled() is read. Use the existing cancellation-safe synchronous
event path or wait for listener completion specifically for
common:BackButtonPressed, while preserving the current JNI_TRUE/JNI_FALSE return
behavior.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro Plus
Run ID: 561baa6d-0b11-4ae1-9746-569a2343891e
📒 Files selected for processing (6)
v3/internal/commands/build_assets/android/app/src/main/java/com/wails/app/MainActivity.javav3/internal/commands/build_assets/android/app/src/main/java/com/wails/app/WailsBridge.javav3/pkg/application/application_android.gov3/pkg/events/events.gov3/pkg/events/events.txtv3/pkg/events/known_events.go
e59606c to
69ca688
Compare
|
Valid observation — addressed in the force-push with an expanded comment explaining the design: Hooks run synchronously (before The code comment and doc now explicitly state: use app.Event.RegisterApplicationEventHook(events.Common.BackButtonPressed, func(e *application.ApplicationEvent) {
e.Cancel() // runs synchronously, reliably suppresses default behavior
})
|
… navigation
The back button was hardcoded to webView.goBack() else exit, breaking
SPA routing (unsaved-changes guards, in-app navigation, search overlays).
Apps had no way to intercept or override the back action.
Add a common:BackButtonPressed event that fires synchronously from
onBackPressed via a new nativeOnBackPressed JNI call. If a Go listener
calls event.Cancel(), the back action is suppressed — the app handles
navigation itself. If no listener cancels, the legacy behavior
(goBack / exit) remains as the default.
Usage:
app.Event.OnApplicationEvent(events.Common.BackButtonPressed, func(e *application.ApplicationEvent) {
// handle back press (e.g. close a modal, navigate in-app)
e.Cancel() // suppress the default goBack/exit
})
69ca688 to
7f3576f
Compare
Description
The Android back button is hardcoded to
webView.goBack()else exit. This breaks SPA routing, unsaved-changes guards, in-app navigation stacks, modal dismissal, and any app that needs to intercept the back action.This emits a cancelable
common:BackButtonPressedevent before performing the default navigation. If a Go listener callsevent.Cancel(), the default action is suppressed and the app handles navigation itself. If no listener cancels (or no listener is registered), the legacy behavior remains unchanged.Implementation: a new synchronous JNI call
nativeOnBackPressed()→ Go invokeshandleApplicationEvent(which processes hooks and listeners) → returns boolean (cancelled or not) → Java honors the result.Type of change
How Has This Been Tested?
android/arm64via NDK) — compiles.The back-button behavior is fully backwards-compatible: apps with no
BackButtonPressedlistener see exactly the same goBack/exit behavior as before.Test Configuration
Checklist:
Notes:
Summary by CodeRabbit
common:BackButtonPressedevent for handling back-button actions.